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>