Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Dec 2003
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Showing decimals on whole numbers?

    Is it possible to have a round number display as #.0 when one is using this to round the number to a decimal:

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

  • #2
    Regular Coder
    Join Date
    Sep 2003
    Posts
    290
    Thanks
    0
    Thanked 0 Times in 0 Posts
    secgap.toFixed(1)

  • #3
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This should work:
    Code:
    secgap = Math.round(secgap*10).toString();
           secgap = secgap.substring(0,secgap.length-1)+'.'+
           secgap.substring(secgap.length-1,secgap.length);
    .....Willy

  • #4
    Regular Coder
    Join Date
    Dec 2003
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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:

    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.

  • #5
    Regular Coder
    Join Date
    Sep 2003
    Posts
    290
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    My way works just as well.
    Code:
    <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

  • #7
    Regular Coder
    Join Date
    Dec 2003
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Indeed they both work perfectly. My bad. Thanks alot!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •