Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How can I convert a Json Number string to different format?

I get the Json numebr like that

Amount": 816673.5789


Here Amountis the column name.

I need to write a function using Javascript. This functing has two parameter one is this 'Amount', another is 'NumberFormate'. I need to convert this 'Amount' as given date 'NumberFormate' and function will return this number.

Here 'NumberFormate' indicate how many number we need to count after point.
Example:

If user given format is like that

##.## 


the the function return number as 8,16,673.57

If user given format is like that

##.### 


the the function return number as 8,16,673.578

Please remind that the number wil be comma separated when it return.
Posted

1 solution

You can use JavaScript build in formatting and regex to do so...
JavaScript
n.toFixed(2).replace(/\d(?=(\d{3})+\.)/, '$&,');

Where n is the number, toFixed(2) truncates the digits after the decimal point and the regex adds comma (,) before each group of 3...
http://www.regexper.com/#%5Cd(%3F%3D(%5Cd%7B3%7D)%2B%5C.)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900