math - Javascript: how to round number to 2 non-zero decimals - Stack Overflow
Without jQuery, how can I round a float number to 2 non-zero decimals (but only when needed - 1.5 instead of 1.50)?
Just like this:
2.50000000004 -> 2.5
2.652 -> 2.65
2.655 -> 2.66
0.00000204 -> 0.000002
0.00000205 -> 0.0000021
I tried this code:
var r = n.toFixed(1-Math.floor(Math.log10(n)));
but n=0.00000205
implies r=0.0000020
, which is in conflict with conditions above.
But n=0.0000020501
implies r=0.0000021
, which is OK, so the error is only for 5 as a last decimal, which should be rounded up.
Without jQuery, how can I round a float number to 2 non-zero decimals (but only when needed - 1.5 instead of 1.50)?
Just like this:
2.50000000004 -> 2.5
2.652 -> 2.65
2.655 -> 2.66
0.00000204 -> 0.000002
0.00000205 -> 0.0000021
I tried this code:
var r = n.toFixed(1-Math.floor(Math.log10(n)));
but n=0.00000205
implies r=0.0000020
, which is in conflict with conditions above.
But n=0.0000020501
implies r=0.0000021
, which is OK, so the error is only for 5 as a last decimal, which should be rounded up.
- 5 Possible duplicate of Round to at most 2 decimal places in JavaScript – Tot Zam Commented Aug 8, 2016 at 20:06
- 1 @PrasadShinde This will round the last 2 examples to 0, which is not what the OP wants. – Arnauld Commented Aug 8, 2016 at 20:10
- It is not a duplicate. I don't want to round to 2 decimal places, I want to round to 2 NON-ZERO decimal places. – core4096 Commented Aug 8, 2016 at 20:42
- 1 The term you're looking for is "rounding to precision". See developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – JJJ Commented Aug 8, 2016 at 20:49
-
2
The reason you get
0.000020
for that is related to the imprecision of the floating point representation.0.0000205
cannot be represented as exactly that value in floating point. For numbers that can, the result is as you would expect. NB: Ad-hoc coded functions that try to do this will probably all suffer from the same problem. – trincot Commented Aug 8, 2016 at 21:40
2 Answers
Reset to default 8This should do what you want:
function twoDecimals(n) {
var log10 = n ? Math.floor(Math.log10(n)) : 0,
div = log10 < 0 ? Math.pow(10, 1 - log10) : 100;
return Math.round(n * div) / div;
}
var test = [
2.50000000004,
2.652,
2.655,
0.00000204,
0.00000205,
0.00000605
];
test.forEach(function(n) {
console.log(n, '->', twoDecimals(n));
});
Thank's for the @Arnauld answer. Just added decimals
parameter.
function roundToDecimals(n, decimals) {
var log10 = n ? Math.floor(Math.log10(n)) : 0,
div = log10 < 0 ? Math.pow(10, decimals - log10 - 1) : Math.pow(10, decimals);
return Math.round(n * div) / div;
}
var numDecimals = 2
var test = [
2.50000000004,
2.652,
2.655,
0.00000204,
0.00000205,
0.00000605
];
test.forEach(function(n) {
console.log(n, '->', roundToDecimals(n, numDecimals));
});
- 2020年世界信息安全大会将于11月24日在蓉举办
- 安卓上市11年 为何说iPhone用户也要感谢它
- 2013年科技行业推出的失败产品(组图)
- 从鼠标到触屏输入 另类角度深度分析PC进化
- 鲍尔默安抚硬件伙伴:Surface只是一个参考设计
- typescript - tag a href download filename nextjs - Stack Overflow
- file permissions - UNITY An error occurred while resolving packages: EPERM: operation not permitted, mkdir - Stack Overflow
- testrigor - Exception upon running tests to run database query - Stack Overflow
- Flutter - individual bloc states - Stack Overflow
- installation - Upgrading to Inno Setup v6 - SignTool=sha1 no longer works - Stack Overflow
- c++ - Camera is tilting when trying to rotate quaternion - Stack Overflow
- c - MPI Program Hangs after MPI_Finalize - Stack Overflow
- kotlin - Android Studio Code Completion not working in test folder in all projects - Stack Overflow
- javascript - Firebase Auth link - Problem with the Google login, no possibility to change to own project name - Stack Overflow
- c++ - Incremental compilation using Makefile - Stack Overflow
- mapping - How to display drawdown contours calculated with the Theis equation to display on an interactive map in Dash with Pyth
- micropython - Bidirectional communication over USB between host PC and Raspberry Pi Pico - Stack Overflow