cofactor
asked on
comma seperator value in javascript
I need a java script function which will put comma separation (currency format) in amounts
Input : 4000000.00
output: 4,000,000.00
Input: 1234567.458
Output:1,234,567.46 (rounded)
Input : 4000000.00
output: 4,000,000.00
Input: 1234567.458
Output:1,234,567.46 (rounded)
Test page : http://jsfiddle.net/Va5Fy/
Need more powerful check here the Decimal adjustment:
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Math/floor
function f(n) {
return (Math.round(n*100)/100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
Need more powerful check here the Decimal adjustment:
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Math/floor
ASKER
This is not working ....
jQuery(document).ready(
function() {
$(".cm_856231kj").each(fun ction(inde x, el) {
var amount=$(el).html();
$(el).html(numberWithComma s(amount)) ;
});
});
whats wrong ?
jQuery(document).ready(
function() {
$(".cm_856231kj").each(fun
var amount=$(el).html();
$(el).html(numberWithComma
});
});
whats wrong ?
whats happening? error message? need more details
Work fine for me : http://jsfiddle.net/bvF7N/
ASKER
please note $(el) is a span element. I am trying to replace span content with comma formatted amount .....however the code below does not work.
jQuery(document).ready(
function() {
var cm_856231kj_arr=$(".cm_856 231kj");
$(".cm_856231kj").each(fun ction(inde x, el) {
var amount=$(el).html();
alert('hello='+amount); <-------I get alert here .
$(el).html(numberWithComma s(amount)) ; .
<-------- value not set ..loop beraks
});
});
jQuery(document).ready(
function() {
var cm_856231kj_arr=$(".cm_856
$(".cm_856231kj").each(fun
var amount=$(el).html();
alert('hello='+amount); <-------I get alert here .
$(el).html(numberWithComma
<-------- value not set ..loop beraks
});
});
span : http://jsfiddle.net/bvF7N/2/
ASKER
How do I debug my code ...whats wrong ?
should I print alert(e1.type) to check if all are span elements ?
should I print alert(e1.type) to check if all are span elements ?
How do I debug my code ...whats wrong ?Your code have nothing wrong but the function from The_Big_Daddy you're using...
So using mine maybe a good idea...
If you LOVE the name of the function, just replace :
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",").toFixed(2);
}
by :function numberWithCommas(n) {
return (Math.round(n*100)/100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
ASKER
@leakim971
There is a issue in your JavaScript. function
when I put a whole number say .... n=124
It gives output 124.00
I want output 124 here.
Can you please modify your code ?
There is a issue in your JavaScript. function
when I put a whole number say .... n=124
It gives output 124.00
I want output 124 here.
Can you please modify your code ?
An issue?
Done : http://jsfiddle.net/bvF7N/7/
Done : http://jsfiddle.net/bvF7N/7/
ASKER
I applied this ...
if(n % 1 != 0){ //n is decimal
return (Math.round(n*100)/100).to Fixed(2).t oString(). replace(/\ B(?=(\d{3} )+(?!\d))/ g, ",");
}else{ // n is whole number
return n.toString().replace(/\B(? =(\d{3})+( ?!\d))/g, ",");
}
How is it ?
if(n % 1 != 0){ //n is decimal
return (Math.round(n*100)/100).to
}else{ // n is whole number
return n.toString().replace(/\B(?
}
How is it ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
return x.toString().replace(/\B(?
}
alert( numberWithCommas( 1234567.458' ) );