[英]Leading and trailing zeros in numbers
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. 当然,它不会在值本身中保留'00',因为该值实际上是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: JavaScript了解我在做什么,但最终实际值将是7,可以通过以下方式证明:
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? 1)有人可以解释为什么这不起作用?
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? 2)JS / NodeJS甚至TypeScript中有没有办法在不使用字符串的情况下执行此操作?
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. 我目前要做的是接收输入,扫描格式并将其存储为单独的属性,然后解析传入的值,因为
parseInt('007.000')
确实有效。 And when the user wants to get this value return it back to the user... in a string.. unfortunately. 并且当用户想要获得该值时,不幸地将其返回给用户...在字符串中。
1) 007.000
is a syntax error because 007 is an octal integer literal , to which you're then appending a floating point part. 1)
007.000
是一个语法错误,因为007是一个八进制整数文字 ,然后你要附加一个浮点部分。 (Try console.log(010)
. This prints 8.) (尝试
console.log(010)
。这打印8.)
2) Here's how you can achieve your formatting using Intl.NumberFormat ... 2)以下是使用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 您可以使用使用字符串函数.split .padStart和.padEnd的aproach
Search on MDN 在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 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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.