I was searching a way to only display two decimals of a variable and I found the .toFixed()
function, but it rounds the final number, I came across this page Display two decimal places, no rounding, where one guy says about this method: .toString().match(/^-?\d+(?:\.\d{0,2})?/)[0]
, which works perfectly fine, but it's too long. I tryed doing this:
function twoDecimals(i) {
i = Number(i.toString().match(/^-?\d+(?:\.\d{0,2})?/)[0]);
}
.toString() makes it a string, so I use Number() to make it back a variable
and then I do this:
var num = 12.34567;
num.twoDecimals(this);
Basically I want to make .twoDecimals()
equal to .toString().match(/^-?\d+(?:\.\d{0,2})?/)[0]
or even better if it has Number()
surrounding all