0

我有代码,我需要到达的数字返回十进制后 1 或 2 位而不是像现在这样的 15,这就是我所拥有的。

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);
    }
}
4

6 回答 6

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);
    }
}
于 2013-07-11T06:22:40.890 回答
1

所以基本上你需要对值进行四舍五入:

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);
    }
}

来源:https ://stackoverflow.com/a/8927144/821056

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

于 2013-07-11T06:34:03.757 回答
1
var num = 5.56789;
var n=num.toFixed(2);
于 2013-07-11T06:25:59.670 回答
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

于 2013-07-11T06:22:42.750 回答
0

我想到了!!现在,每当我输入 .tofixed 时,它都不起作用,但是就像我说的那样,这是脚本中稍后代码的函数,所以 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);
    }
}
于 2013-07-14T04:22:28.743 回答
-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).

于 2013-07-11T06:23:11.187 回答