I am working on a project where I require to format incoming numbers in the following way:
###.###
However I noticed some results I didn't expect. The following works in the sense that I don't get an error:
console.log(07);
// or in my case:
console.log(007);
I am working on a project where I require to format incoming numbers in the following way:
###.###
However I noticed some results I didn't expect. The following works in the sense that I don't get an error:
console.log(07);
// or in my case:
console.log(007);
Of course, it will not retain the '00' in the value itself, since that value is effectively 7.
The same goes for the following:
console.log(7.0);
// or in my case:
console.log(7.000);
JavaScript understands what I am doing, but in the end the actual value will be 7, which can be proven with the following:
const leadingValue = 007;
const trailingValue = 7.00;
console.log(leadingValue, trailingValue); // both are exactly 7
But what I find curious is the following: the moment I combine these two I get a syntax error:
// but not this:
console.log(007.000);
1) Can someone explain why this isn't working?
I'm trying to find a solution to store numbers/floats with the exact precision without using string.
2) Is there any way in JS/NodeJS or even TypeScript to do this without using strings?
What I currently want to do is to receive the input, scan for the format and store that as a separate property and then parse the incoming value since parseInt('007.000')
does work. And when the user wants to get this value return it back to the user... in a string.. unfortunately.
2 Answers
Reset to default 191) 007.000
is a syntax error because 007 is an octal integer literal, to which you're then appending a floating point part. (Try console.log(010)
. This prints 8.)
2) Here's how you can achieve your formatting using Intl.NumberFormat...
var myformat = new Intl.NumberFormat('en-US', {
minimumIntegerDigits: 3,
minimumFractionDigits: 3
});
console.log(myformat.format(7)); // prints 007.000
Hi
You can use an aproach that uses string funtions .split .padStart and .padEnd
Search on MDN
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd
Here you have an example:
const x = 12.1;
function formatNumber( unformatedNumber) {
const desiredDecimalPad = 3;
const desiredNonDecimalPad = 3;
const unformatedNumberString = unformatedNumber.toString();
const unformatedNumberArr = unformatedNumberString.split('.');
const decimalStartPadded = unformatedNumberArr[0].padStart(desiredDecimalPad, '0');
const nonDecimalEndPadded = unformatedNumberArr[1].padEnd(desiredNonDecimalPad, '0');
const formatedNumberString = decimalStartPadded + '.' + nonDecimalEndPadded;
return formatedNumberString;
}
console.log(formatNumber(x))
07
or007
in the output. The way it "works" is that it doesn't throw an error. And I don't know why you'd need to ever write007.000
- that is apparently a syntax error (which, TBH, I didn't know until now) but more importantly, it's useless as you're at best going to merely get7
out. – VLAZ Commented May 17, 2019 at 12:387
and7.0
and7.000
are the same value. That's true for any language I know of - primitives require separate formatting. – VLAZ Commented May 17, 2019 at 12:400
s. Internally it is still the same, but why don't just use strings? If you want to display the number a string is fine and it also makes no difference in calculating if you do3 * 7.00
or3 * 7
and so on... – messerbill Commented May 17, 2019 at 12:41