Forum Moderators: open

Message Too Old, No Replies

Javascript Change Digits

         

matthewamzn

8:40 pm on Mar 4, 2006 (gmt 0)

10+ Year Member



I'm using a javascript to automatically change kilometers to miles, on my website. The only problem is it produces some very awkward looking numbers with many decimal points. Is there a way to make the product a nice round number?

<script language="JavaScript" type="text/javascript">
var prod = ('##miles##' * 1.609344);
document.write(prod, " kms");
</script>

johnl

10:03 pm on Mar 4, 2006 (gmt 0)

10+ Year Member



hi,
insert

prod=Math.round(prod*100)/100

as the third line in your script.

mrhoo

5:29 am on Mar 5, 2006 (gmt 0)

10+ Year Member



johnl's code is fine for a round number, but if you want to include a fixed # of decimal places you can use
toFixed():
(number).toFixed(2)

matthewamzn

4:03 pm on Mar 10, 2006 (gmt 0)

10+ Year Member



That worked great. Is there a code to deliminate 1000's with a comma?

ie 1,225,365 istead of 1225365

Bernard Marx

8:00 pm on Mar 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A: 1225365.4567
B: 1225365

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 */

Bernard Marx

8:06 pm on Mar 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could forget the above. If you are dealing with money (ie want 2 decimal places)then you can simply use the toLocaleString method:

alert( (1234567.34567).toLocaleString() )

matthewamzn

8:43 pm on Mar 10, 2006 (gmt 0)

10+ Year Member



How can I combine the script above with mine?

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>

Bernard Marx

10:02 pm on Mar 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The script isn't making Javascript sense at the moment.

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.