...

View Full Version : Putting commas in output



florida
06-09-2003, 02:41 PM
I have an output but need to put a comma for the every three numbers.
For example:
220469.06 Should be 220,469.06

And this:
2469.06 should be 2,469.06

and this: 234.03 should stay 234.03

And this: 2324566.00 should be 2,324,566.00

My attempt isnt working:


number = form1.dollarTotal.value;
newDollarTotal = number.subtring(0,3)+","+number.subtring(4,6);

Graeme Hackston
06-09-2003, 03:38 PM
I've seen regular expressions that do this but I don't understand them so I wrote this



//Rounding example = Make_Number_String_with_Commas(87954778464.7771,'round_result')

function Make_Number_String_with_Commas(input_number, round_results) {

if (round_results) {
input_number = Math.round(input_number)
}

var num_str = '*' + input_number + '*'
num_str = num_str.substring(1, num_str.length - 1)

var isDecimal = false
if (num_str.indexOf('.') != -1) {
isDecimal = true
var decimal_array = num_str.split('.')
num_str = decimal_array[0]
}

var num_str_length = num_str.length
var array_1 = new Array()

for (var i=0;i<num_str_length;i++) {
array_1[array_1.length] = num_str.charAt(i)
}

var array_1_reversed = array_1.reverse()
array_1_reversed_length = array_1_reversed.length

var array_2 = new Array()

for (var i=0;i<array_1_reversed_length;i++) {
if (i == 0) {
array_2[array_2.length] = array_1_reversed[i]
} else {
var divide_by_3_str = '*' + i/3 + '*'
divide_by_3_str = divide_by_3_str.substring(1, divide_by_3_str.length - 1)
if (divide_by_3_str.indexOf('.') == -1) {
array_2[array_2.length] = array_1_reversed[i] + ','
} else {
array_2[array_2.length] = array_1_reversed[i]
}
}
}

var num_comma_str = ''

array_2 = array_2.reverse()

for (var i=0;i<array_2.length;i++) {
num_comma_str += array_2[i]
}

if (isDecimal) {
num_comma_str = num_comma_str + '.' + decimal_array[1]
}

return num_comma_str
return null
}


I don't think you can do anything about scientific notation numbers but I hope I'm wrong?

sage45
06-09-2003, 04:06 PM
Here is a shorter version:



//-- Shows number in $$xxx,xxx.xx format and pads left side with blanks.
function currency(anynum,width) {
anynum=eval(anynum)
workNum=Math.abs((Math.round(anynum*100)/100));workStr=""+workNum
if (workStr.indexOf(".")==-1){workStr+=".00"}
dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0
pStr=workStr.substr(workStr.indexOf("."))
while (pStr.length<3){pStr+="0"}

//--- Adds comma in thousands place.
if (dNum>=1000) {
dLen=dStr.length
dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
}

//-- Adds comma in millions place.
if (dNum>=1000000) {
dLen=dStr.length
dStr=parseInt(""+(dNum/1000000))+","+dStr.substring(dLen-7,dLen)
}
retval=dStr+pStr
if (anynum < 0) {
retval=retval.substring(1,retval.length)
retval="("+retval+")"
}
retval = "$"+retval
//--Pad with leading blanks to better align numbers.
while (retval.length<width){retval=" "+retval}

return retval
}

florida
06-09-2003, 05:19 PM
Thanks

tutash
07-21-2006, 04:31 PM
Written in actionScript, but it works its all ECMA based, so whatever...


String.prototype.currencyFormat = function() {
isNaN(parseFloat(this)) ? c = 0 : c = parseFloat(this);
v = (Math.round(Number(c) * 100) / 100).toString();
g = v.split(".");
t = g[0].split("");
for (i = t.length - 1; i > -1; i -= 3) {
i != t.length - 1 ? t[i] += "," : null;
}
f = String(Math.round(Math.abs(v) % 1 * 100));
f.length == 1 ? f = ".0" + f : f = "." + f;
return t.join("") + f;
};
//
q = "-1999.999gfhd";
//
trace(q.currencyFormat());
//output: -2,000.00


This is about as simple as I can get it. Currency formatting, and comma insertion.

enjoy

-tutash

tonyp12
02-03-2007, 12:00 AM
here is a simple one I cooked up, first one is regular and the second one is for currency

var myamount = 12345678.2134
myamount= myamount.toString()
var mylen = myamount.length
var j = myamount.indexOf('.')>0 ? mylen-myamount.indexOf('.') : 0
for (var i=3+j; i<18+j; i +=3){
if (mylen>i){myamount= myamount.slice(0,mylen-i) + "," + myamount.slice(mylen-i)}
}

myamount is now 12,345,678.2134

line 1: We set the value as numeral as that is probably how we get it in a real application.
line 2: Convert to string for manipulation.
line 3: How many letters are the in this string.
line 4: Is there a decimal point?
line 5: loop 5 times, should be enough as that is quadrillion
line 6: merge in "," at right places.

Currency version that force two decimals


function tocurrency(myamount){
myamount= Math.round(myamount * 100).toString();
var mylen = myamount.length;
for (var i=5; i<20; i +=3){
if (mylen>i){myamount= myamount.slice(0,mylen-i) + "," + myamount.slice(mylen-i)}
}
mylen = myamount.length;
if (mylen<2) {myamount = "0" + myamount;mylen++}
return "$" + myamount.slice(0,mylen-2) + "." + myamount.slice(mylen-2);
}

If you called the function with this example
document.getElementById('mytotalfield').value= tocurrency(6000 + 578)
it would show $6,578.00

I have tested above function with a string and it works OK without parseFloat it first.
I take it that Math.round() does it's own parseFloat as "1234" * 100 should not work.
But take in account that strings "1234" + "10" = "123410" and not 1244 in any pre calculations.

Here is a split each digit to array version.


function tocurrency(d){
var myamount=[];
myamount= Math.round(d * 100).toString().split("");
var mylen = myamount.length;
if (mylen<2) {myamount.splice(0,0,".","0")}
else {myamount.splice(mylen-2,0,".")}
// if (mylen<3) {myamount.splice(0,0,"0")} // If you want .50 to show 0.50
for (var i=5; i<20; i +=3){
if (mylen>i){myamount.splice(mylen-i,0,",")}
}
return "$" + myamount.join("");
}

Philip M
07-12-2007, 07:41 PM
This works well using regular expressions:-

<script type = "text/javascript">

var num = 1234567890123;

document.write('<span id="count"></span>');

document.getElementById('count').innerHTML = addCommas(String(num));

function addCommas(nStr) {
var rgx = /(\d+)(\d{3})/;
while (rgx.test(nStr)) {
nStr = nStr.replace(rgx, '$1' + ',' + '$2');
}
return nStr;
}

</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum