gpt4 book ai didi

javascript - 为什么 "_"从 javascript 中的数字中删除?

转载 作者:行者123 更新时间:2023-12-01 11:16:45 26 4
gpt4 key购买 nike

我尝试在 Chrome 控制台中输入以下代码:

var a = 16_11;

它不在"'里面。而且a的输出是1611而不是16_11。为什么是_ 被删除?

ConsoleOutput

最佳答案

你有一个 numeric separator这是 V8 v7.5/Chrome 75 中的提案和实际交付。

This feature enables developers to make their numeric literals more readable by creating a visual separation between groups of digits. Large numeric literals are difficult for the human eye to parse quickly, especially when there are long digit repetitions. This impairs both the ability to get the correct value / order of magnitude...

1000000000   // Is this a billion? a hundred millions? Ten millions?
101475938.38 // what scale is this? what power of 10?

...but also fails to convey some use-case information, such as fixed-point arithmetic using integers. For instance, financial computations often work in 4- to 6-digit fixed-point arithmetics, but even storing amounts as cents is not immediately obvious without separators in literals:

const FEE = 12300;
// is this 12,300? Or 123, because it's in cents?

const AMOUNT = 1234500;
// is this 1,234,500? Or cents, hence 12,345? Or financial, 4-fixed 123.45?

Using underscores (_, U+005F) as separators helps improve readability for numeric literals, both integers and floating-point (and in JS, it's all floating-point anyway):

1_000_000_000           // Ah, so a billion
101_475_938.38 // And this is hundreds of millions

let fee = 123_00; // $123 (12300 cents, apparently)
let fee = 12_300; // $12,300 (woah, that fee!)
let amount = 12345_00; // 12,345 (1234500 cents, apparently)
let amount = 123_4500; // 123.45 (4-fixed financial)
let amount = 1_234_500; // 1,234,500

Also, this works on the fractional and exponent parts, too:

0.000_001 // 1 millionth
1e10_000 // 10^10000 -- granted, far less useful / in-range...

更多来源:

var a = 1_000;

console.log(a);

关于javascript - 为什么 "_"从 javascript 中的数字中删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59512450/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com