...

View Full Version : Showing decimals on whole numbers?



NanakiXIII
02-02-2004, 05:51 PM
Is it possible to have a round number display as #.0 when one is using this to round the number to a decimal:



secgap = Math.round(secgap*10)/10;

Jeff Mott
02-02-2004, 06:17 PM
secgap.toFixed(1)

Willy Duitt
02-02-2004, 06:17 PM
This should work:

secgap = Math.round(secgap*10).toString();
secgap = secgap.substring(0,secgap.length-1)+'.'+
secgap.substring(secgap.length-1,secgap.length);

.....Willy

NanakiXIII
02-02-2004, 06:26 PM
Both of those suggestions seem to have the same effect as what I already had. Maybe I'm using them incorrectly.

This is my old code:



function secage()
{
var birthdate = new Date("January 31, 2004 08:30:00");
var now = new Date();
var milsec = (now.getTime() - birthdate.getTime());
var secgap = milsec / 1000;
secgap = Math.round(secgap*10)/10;
document.ageform.secage.value = "Age in seconds: " + secgap;
var timeout = setTimeout("secage()", 50);
}


In which I replaced secgap = Math.round(secgap*10)/10; with your suggestions.

Jeff Mott
02-02-2004, 06:36 PM
As I understand it, you wanted to display a ".0" even if the number ends up being an integer. Is that correct? If so then toFixed() should do just that.

e.g.,
alert( Number(1).toFixed(1) ); // alerts 1.0

Willy Duitt
02-02-2004, 06:46 PM
My way works just as well.

<script type="text/javascript">
function secage()
{
var birthdate = new Date("January 31, 2004 08:30:00");
var now = new Date();
var milsec = (now.getTime() - birthdate.getTime());
var secgap = milsec / 1000;
secgap = Math.round(secgap*10).toString();
secgap = secgap.substring(0,secgap.length-1)+'.'+
secgap.substring(secgap.length-1,secgap.length);
document.getElementById('time').innerHTML = secgap;
var timeout = setTimeout("secage()", 100);
}
</script>
</HEAD>

<BODY onload="secage()">
<span id="time"></span>

.....Willy

NanakiXIII
02-02-2004, 07:36 PM
Indeed they both work perfectly. My bad. Thanks alot!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum