最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Leading and trailing zeros in numbers - Stack Overflow

programmeradmin3浏览0评论

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.

Share Improve this question edited May 17, 2019 at 13:01 Kai asked May 17, 2019 at 12:32 KaiKai 1,2251 gold badge9 silver badges18 bronze badges 7
  • 2 "The following works" can you define what you mean by that? Because you don't get the number 07 or 007 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 write 007.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 get 7 out. – VLAZ Commented May 17, 2019 at 12:38
  • 2 As 2) - you cannot escape the strings. JS numerics will only be represented how JS wants to. It doesn't capture the format you used, so 7 and 7.0 and 7.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:40
  • i guess this is only displayed without those 0s. 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 do 3 * 7.00 or 3 * 7 and so on... – messerbill Commented May 17, 2019 at 12:41
  • Related: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… – Paleo Commented May 17, 2019 at 12:43
  • @VLAZ tbh, I was just experimenting with what I can do and not and just encountered this one. I understand that in the end 7.0 will be treated as 7. I'm looking for a way to prevent storing properties like 'quantity' as a string value, since that doesn't make sense to me. – Kai Commented May 17, 2019 at 12:50
 |  Show 2 more comments

2 Answers 2

Reset to default 19

1) 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))

发布评论

评论列表(0)

  1. 暂无评论