Forum Moderators: open
<script language="JavaScript" type="text/javascript">
var prod = ('##miles##' * 1.609344);
document.write(prod, " kms");
</script>
Apply toFixed(2), then split on the decimal point..
A: ["1225365","46"]
B: ["1225365"]
Take the first element, split into chars..
[1,2,2,5,3,6,5]
reverse, join..
"5635221"
replace every full group of 3 digits with itself plus a comma
"563,522,1"
split into chars, reverse, join,
then, if there is a 2nd element in the decimal point split,
add that after a point.
A: "1,225,365.46"
B: "1,225,365"
Number.prototype.fullFormat = function(decPlaces)
{
var parts = this.toFixed(decPlaces).split(".");
return (
parts[0].split("").reverse().join("").replace(/\d{3}/g,"$&,")
.split("").reverse().join("") + (parts[1]?"."+parts[1]:"")
);
}alert( (1225365.4567).fullFormat(2) ) /* A */
alert( (-1225365.4567).fullFormat(0) ) /* B */
Number.prototype.fullFormat = function(decPlaces)
{
var parts = this.toFixed(decPlaces).split(".");
return (
parts[0].split("").reverse().join("").replace(/\d{3}/g,"$&,")
.split("").reverse().join("") + (parts[1]?"."+parts[1]:"")
);
}
alert( (1225365.4567).fullFormat(2) ) /* A */
alert( (-1225365.4567).fullFormat(0) ) /* B */
<script language="JavaScript" type="text/javascript">
var prod = ('##miles##' * 1.609344);
prod=Math.round(prod*1)/1
document.write(prod, " kms");
</script>
var milesToKm = 1.609344;
var miles = 15000; /* test value */document.write( (miles*milesToKm).fullFormat(0)+" km" );
/* or without need for fullFormat */
document.write( (miles*milesToKm).toLocaleString().replace(/[\.,][\d]+$/,'') +" km" )
/[\.,][\d]+$/ From right to left..
$ End of string
[/d]+ One or more digits [\.,] Dot or comma (allows for "foreign" formats) Replace this with an empty string, and we have effectively rounded to the nearest unit, but keeping the 1000 separators.