Forum Discussion
Help with Java Script to format numbers
Hi Math
The script brought up the dollar sign but it didnt put in any commas. What is the script for putting in commas as well please?
Cheers
Kim
var player=GetPlayer();
varTotAvailSav=playerGetVar("TotAvailSav");
varTotAvailSav=TotAvailSav.toFixed(0).replace(/\d(?=(\d{3})+\.)/g, '$&,');
vartxtvalue=TotAvailSav.toString();
player.SetVar("txtTotAvailSav",txtvalue);
- MathNotermans-94 years agoCommunity Member
Im not quite sure what's the issue. Might be the regex replace that removes all comma's .... my Regex is not that good. Whats supposed to happen ?
Here you can construct and test any Regex you want.
https://regex101.com/ - SteveGannon4 years agoCommunity Member
Kim,
If you do not want any decimal places, try replacing your third line of code with this line:
var TotAvailSav=TotAvailSav.toFixed().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$&,');
If you do want two decimal places, keep your current third line but change...
toFixed(0)
...to...
toFixed(2)
- MathNotermans-94 years agoCommunity Member
Regex Master Steve to the rescue ;-)
Related Content
- 10 months ago
- 7 months ago
- 4 months ago
- 2 months ago