0

I have code where I need the number that arrives to return 1 or 2 digits after decimal not 15 like it does now, here is what I have.

function GetDiff (dt) { 
    sMins = " Min";
    sHours = " Hrs";
    sDays = " Days";

    if ( Math.abs (DateDiff ("n", now, dt)) < 1440 ) {
        if ( Math.abs (DateDiff ("n", now, dt)) <= 60 ) {
            return (Math.abs (DateDiff ("n", now, dt)) + sMins);
        }
        else
        {
            return (Math.abs (DateDiff ("n", now, dt)/60) + sHours);
        }
    }
    else
    {
        return (Math.abs (DateDiff ("n", now, dt)/1440) + sDays);
    }
}
jahroy
  • 20,588
  • 9
  • 52
  • 104
chriswiec
  • 781
  • 1
  • 6
  • 17
  • 1
    Use google: ["_javascript round number two places_"](http://stackoverflow.com/q/1726630/778118). – jahroy Jul 11 '13 at 06:24
  • Another [useful link...](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FNumber%2FtoFixed) and [another...](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision). – jahroy Jul 11 '13 at 06:28
  • When I hover over the upvote arrow it says: "_This question shows research effort..._" Not sure why anybody would choose to click on that button after reading this question. – jahroy Jul 11 '13 at 06:34

6 Answers6

4

You can use .toFixed(2) to format a number to 2 decimal places.

Note that .toFixed() returns a string so if you want to work with the result as a number again, you'll need to parseFloat().

function GetDiff (dt) { 
    sMins = " Min";
    sHours = " Hrs";
    sDays = " Days";

    if ( Math.abs (DateDiff ("n", now, dt)) < 1440 ) {
        if ( Math.abs (DateDiff ("n", now, dt)) <= 60 ) {
            return (Math.abs (DateDiff ("n", now, dt)) + sMins).toFixed(2);
        }
        else
        {
            return (Math.abs (DateDiff ("n", now, dt)/60) + sHours).toFixed(2);
        }
    }
    else
    {
        return (Math.abs (DateDiff ("n", now, dt)/1440) + sDays).toFixed(2);
    }
}
MrCode
  • 59,851
  • 9
  • 76
  • 106
  • its pulling the date out of a program it is connected to and as I thought your code would work it does not. Its pulling a Due Date out of a program. I got it to work with this although it only returns 1 digit after decimal. I tried your code screen goes blank. return ((Math.floor(Math.abs (DateDiff ("n", now, dt)/1440)*10)/10) + sDays) – chriswiec Jul 11 '13 at 08:04
  • I don't see your call to `toFixed()` – MrCode Jul 11 '13 at 08:06
  • its not there when I put in the .toFixed(2); the sections with that data disappeared. – chriswiec Jul 11 '13 at 08:17
  • Make sure that you're only applying `toFixed(2)` to a number, not a string or anything else... – MrCode Jul 11 '13 at 08:18
  • maybe I need to implement the .parsefloat() how would I do that? sorry this is all so new to me. – chriswiec Jul 11 '13 at 08:19
  • Oh ya I think its definitely a string because I use the function GetDiff later down on in the code. for ex. "if ( DateDiff ("n", TestLate, now) >= 0 ) { ReviewIcon = ""; TestIcon = "Late By " + GetDiff (TestLate);}" – chriswiec Jul 11 '13 at 08:21
  • If you have a string, for example `var str = '1.9876543';` then use `console.log( parseFloat(str).toFixed(2) );` – MrCode Jul 11 '13 at 09:03
1
var num = 5.56789;
var n=num.toFixed(2);
Michael Leiss
  • 4,944
  • 3
  • 16
  • 25
1

So basically you need to round the values:

function GetDiff (dt) { 
    sMins = " Min";
    sHours = " Hrs";
    sDays = " Days";

    if ( Math.abs (DateDiff ("n", now, dt)) < 1440 ) {
        if ( Math.abs (DateDiff ("n", now, dt)) <= 60 ) {
            return (Math.abs (DateDiff ("n", now, dt)) + sMins).toFixed(2);
        }
        else
        {
            return (Math.abs (DateDiff ("n", now, dt)/60) + sHours).toFixed(2);
        }
    }
    else
    {
        return (Math.abs (DateDiff ("n", now, dt)/1440) + sDays).toFixed(2);
    }
}

Source: https://stackoverflow.com/a/8927144/821056

http://www.mredkj.com/javascript/numberFormat.html

Community
  • 1
  • 1
Viral Jain
  • 966
  • 1
  • 11
  • 28
0

Here's how to round a number to two decimals

var original=28.453
var result=Math.round(original*100)/100  //returns 28.45

Taken from: http://www.javascriptkit.com/javatutors/round.shtml

Winks
  • 369
  • 1
  • 3
  • 15
0

I figured it out!! Now Whenever I entered .tofixed it wouldn't work, but like I said this is a function for code later on in the script so idk.

                    function GetDiff (dt) { 
        sMins = " Min";
    sHours = " Hrs";
    sDays = " Days";

    if ( Math.abs (DateDiff ("n", now, dt)) < 1440 ) {
        if ( Math.abs (DateDiff ("n", now, dt)) <= 60 ) {
            return (Math.abs (DateDiff ("n", now, dt)) + sMins);
        }
        else
        {
            return ((Math.floor(Math.abs (DateDiff ("n", now, dt)/60)*10)/10) + sHours);
        }
    }
    else
    {
            return ((Math.floor(Math.abs (DateDiff ("n", now, dt)/1440)*10)/10) + sDays);
    }
}
chriswiec
  • 781
  • 1
  • 6
  • 17
-1

Use a DecimalFormatter:

double number = 0.9999999999999;
DecimalFormat numberFormat = new DecimalFormat("#.00");
System.out.println(numberFormat.format(number));

Will give you "0.99". You can add or subtract 0 on the right side to get more or less decimals.

Or use '#' on the right to make the additional digits optional, as in with #.## (0.30) would drop the trailing 0 to become (0.3).