Fusion Lifecycle Forum
Welcome to Autodesk’s Fusion Lifecycle Forums. Share your knowledge, ask questions, and explore popular Fusion Lifecycle topics.

Fusion Lifecycle Forum

Reply
Post 1 of 4
Accepted Solution

Number 1234567 to 1,234,567?

130 Views, 3 Replies
11-07-2016 02:20 PM

This is more of a javascript question.  Does anyone have an easy way to convert a number like 1234567 to 1,234,567 format?  

 

I tried using toLocaleString() but it doesn't seem to work.  I then tried using a regex but couldn't make it work either.  I then thought about adding commas to every third digit using for loop before I stopped myself and reached out for help.

Tony,

 

This one worked nicely.  Thanks.

 

item.NICE_FIELD = uglyField.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");

Post 2 of 4

Re: Number 1234567 to 1,234,567?

11-07-2016 04:06 PM in reply to: leungjcp

There are a couple of ways of formatting numbers

 

You can use a computed field like this:

TO_CHAR(UGLY_FIELD, 'FM999,999,999,990D00')

 

Or

 

You can use script a a regular expression like this

item.NICE_FIELD = uglyField.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");

 

I tried both of these and they seem to work.

 

Post 3 of 4

Re: Number 1234567 to 1,234,567?

11-07-2016 04:11 PM in reply to: tony.mandatori
Ah.. Thanks toni. I had tried the regular expressions and had something similar. I'll give it a go in the morning. I like the computed one cause i don't have to mess around with the code. Thanks.

[http://www.nucap.com/images/image001.jpg] Jason Leung - Design Engineer
NUCAP Industries Inc.
3370 Pharmacy Avenue, Toronto ON
Ph.416 494 1444 FAX.416 494 2505
Jason.Leung@nucap.com

Please visit us at www.nucap.com www.gripmetal.com



[http://www.nucap.com/images/banner-nrx.png]
Post 4 of 4

Re: Number 1234567 to 1,234,567?

11-07-2016 07:54 PM in reply to: leungjcp

Tony,

 

This one worked nicely.  Thanks.

 

item.NICE_FIELD = uglyField.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post


Couldn’t make it to Autodesk University? Some of the Fusion Lifecycle Classes are available to watch on-demand:
http://autode.sk/2gJmBy3