I have an number I开发者_如何学Go want convert its format to xxx xxx xxx,xx
(will use for display in html page)
Example
1069.83
to
1 069,83
OR something like this
13761000.00
to
13 761 000,00
In JavaScript are the build-in function or the ways to do this?
If this is your data pattern you can enhance this function: ( see it here : http://jsfiddle.net/DSJuL/1/ )
var number = 13761000.00;
alert(formatNum(number));
function formatNum(number)
{
var newNum = "";
var oldNumStr = number + "";
var done = 0;
var parts = oldNumStr.split(".");
var newPart1 = "";
var newPart2 = parts[1];
for(var j= parts[0].length -1 ;j >= 0;j--)
{
newNum = parts[0][j] + newNum;
done++;
if((done%3) == 0)
newNum = " " + newNum;
}
newNum = (newPart2)? (newNum + "," + newPart2) : newNum + ",00" ;
return newNum;
}
If using jQuery is an option you can check the globalization plugin. It supports number formatting nicely. Check this out.
If you can't use jQuery - check this.
精彩评论