How to Get number with 2 decimal and not rounding using javascript ?
i try
var i = "1234.666";
var xxx = Math.floor(i * 100) / 100
alert(xxx);
AND
var i = "1234.666";
function myToFixed(i, digits) {
var pow = Math.pow(10, digits);
return Math.floor(i * pow) / pow;
}
var xxx = myToFixed(i, 2)
alert(xxx);
it's work.
But when i declare var i = "1234";
i want to get "1234.00"
How to do that ?
Not rounding is an unusual requirement, and it's too bad because if you were okay with rounding, you could just use toFixed
:
var num = 1234.5678;
snippet.log(num.toFixed(2)); // "1234.57" -- note the rounding!
var num = 1234.5678; snippet.log(num.toFixed(2)); // "1234.57" -- note the rounding!
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
(And no, you can't just do .toFixed(3)
and then chop off the last digit; rounding doesn't necessarily only change the last digit, consider using toFixed(3)
on 1111.9999
, which gives you 1112.000
. Just chopping off the last digit of that still gives you a rounded result.)
To do it without rounding, I don't think there's much available but brute force:
// Assumes `digits` is never more than 20
function toFixedNoRounding(num, digits) {
var parts = String(num).split('.');
if (digits <= 0) {
return parts[0];
}
var fractional = (parts[1] || "0") + "000000000000000000000";
return parts[0] + "." + fractional.substring(0, digits);
}
// Assumes `digits` is never more than 20 function toFixedNoRounding(num, digits) { var parts = String(num).split('.'); if (digits <= 0) { return parts[0]; } var fractional = (parts[1] || "0") + "000000000000000000000"; return parts[0] + "." + fractional.substring(0, digits); } var num, notRounded; num = 1234; notRounded = toFixedNoRounding(num, 2); snippet.log(num + " => " + notRounded); num = 1234.5678; notRounded = toFixedNoRounding(num, 2); snippet.log(num + " => " + notRounded); num = 1234.1; notRounded = toFixedNoRounding(num, 2); snippet.log(num + " => " + notRounded); num = 1234.999999; notRounded = toFixedNoRounding(num, 2); snippet.log(num + " => " + notRounded);
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
Or you can use indexOf
, but it seems a lot clunkier:
// Assumes `digits` is never more than 20
function toFixedNoRounding(num, digits) {
var str = String(num);
var n = str.indexOf(".") + 1;
if (n === 0) {
str += ".";
n = str.length;
}
n = digits <= 0 ? n - 1 : n + digits;
if (str.length !== n) {
str = (str + "00000000000000000000").substring(0, n);
}
return str;
}
// Assumes `digits` is never more than 20 function toFixedNoRounding(num, digits) { var str = String(num); var n = str.indexOf(".") + 1; if (n === 0) { str += "."; n = str.length; } n = digits <= 0 ? n - 1 : n + digits; if (str.length !== n) { str = (str + "00000000000000000000").substring(0, n); } return str; } var num, notRounded; num = 1234; notRounded = toFixedNoRounding(num, 2); snippet.log(num + " => " + notRounded); num = 1234.5678; notRounded = toFixedNoRounding(num, 2); snippet.log(num + " => " + notRounded); num = 1234.1; notRounded = toFixedNoRounding(num, 2); snippet.log(num + " => " + notRounded); num = 1234.999999; notRounded = toFixedNoRounding(num, 2); snippet.log(num + " => " + notRounded);
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
var y = "1234.045";
var s = number_with_2_decimal(y);
alert(s);
function number_with_2_decimal(a) {
var x = Math.floor(a * 100) * 0.01;
var str = String(x);
var n = str.indexOf('.');
if (n === -1) {
str += '.00';
};
return str;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.