JavaScript: Trim .toPrecision() trailing zeros
Divide by 1 after using toPrecision. Javascript will trail the zeros, and there's no regexes needed.
only all-zero decimals
eval('4*1').toPrecision(10).replace(/\.0+$/,"")
zero ending decimals:
eval('4.5*1').toPrecision(10).replace(/\.([^0]+)0+$/,".$1")
edit: handle both all zero and ending zero cases
EDIT: if you will ALWAYS use .toPrecision() first, and thus always have a ".", you can trim off any trailing zeros:
eval('4.5*1').toPrecision(10).replace(/0+$/,"")
EDIT: handling trailing decimals:
eval('4.5*0').toPrecision(10).replace(/\.?0+$/,"")
The following replace()
call will replace all trailing 0
s and a trailing .
, if present:
eval(expression).toPrecision(10).replace(/(?:\.0+|(\.\d+?)0+)$/, "$1")
How does it work?
/(?:\.0+|(\.\d+?)0+)$/
looks for \.0+
or (\.\d+?)0+
at the end of the string ($
). The ?:
prevents \.0+|(\.\d+?)0+
from being "captured".
\.0+
will match .
followed by any number of 0
s.
(\.\d+?)0+
will match .
followed by at least one digit followed by at least one 0
. The ?
makes sure the 0+
matches as many 0
s as possible. The parentheses "capture" the non-0
s.
The second parameter on replace()
is the new string to replace what was matched. The $1
is a variable of sorts and tells replace()
to replace the matched value with the first captured value (because 1
was after the $
). In this case, the first captured value was whatever \.\d+?
matched.
So, in the end:
.
followed by any number of0
s will be discarded.
followed by non-0
s followed by0
s will have the0
s discarded
Examples
For comparison of methods for precisions of 1
, 2
, and 3
for .40*1
, 4*1
, and 40*1
, see below. First (bold) item in each group is this method. Red items are those that don't match expectations.
var tests = [
'.04*1',
'.40*1',
'4*1',
'40*1'
];
var results = {};
for (var i = 0; i < tests.length; i += 1) {
results[i] = {};
for (var p = 1; p < 3; p += 1) {
results[i][p] = {};
results[i][p][0] = {
'output': eval(tests[i]).toPrecision(p).replace(/(?:\.0+|(\.\d+?)0+)$/, "$1"),
'regex': '/(?:\.0+|(\.\d+?)0+)$/, "$1"'
};
results[i][p][1] = {
'output': eval(tests[i]).toPrecision(p).replace(/\.0+$/, ""),
'regex': '/\.0+$/, ""'
};
results[i][p][2] = {
'output': eval(tests[i]).toPrecision(p).replace(/\.([^0]+)0+$/, ".$1"),
'regex': '/\.([^0]+)0+$/, ".$1"'
};
results[i][p][3] = {
'output': eval(tests[i]).toPrecision(p).replace(/0+$/, ""),
'regex': '/0+$/, ""'
};
results[i][p][4] = {
'output': eval(tests[i]).toPrecision(p).replace(/\.?0+$/, ""),
'regex': '/\.?0+$/, ""'
};
}
}
for (var i in results) {
$("#result").append("<h1>" + tests[i] + "</h1>");
for (var p in results[i]) {
var expected = null;
for (var t in results[i][p]) {
var div = $("<div></div>");
if (t == 0) {
expected = results[i][p][t].output;
div.addClass("expected");
} else if (results[i][p][t].output !== expected) {
div.addClass("invalid");
}
div.append("P" + p + ": " + results[i][p][t].output);
div.append(" <small>" + results[i][p][t].regex + "</small>");
$("#result").append(div);
}
$("#result").append("<br>");
}
}
body { font-family: monospace; }
.expected { font-weight: bold; }
.invalid { color: red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="result"></div>