我可以使用tolocalestring()在不舍入的情况下截断数字吗?

发布于 2025-02-09 14:13:39 字数 501 浏览 0 评论 0 原文

我想使用 tolocalestring()截断 9.95 9.9 。问题是 tolocalestring()始终将数字四舍五入为 10.0

>> (9.95).toLocaleString('en-US', {minimumFractionDigits: 1, maximumFractionDigits: 1})
"10.0"
>> (9.95).toLocaleString('en-US', {minimumFractionDigits: 1, maximumFractionDigits: 1, roundingMode: "trunc"})
"10.0"

我想将其用于DENO,但问题是关于JavaScript API,不仅与Deno有关。我做错了吗?

我在脚本(9.95).toString()中使用了此内容。子弦(0,3)因为舍入问题。

I would like to truncate 9.95 to 9.9 using toLocaleString(). The problem is that toLocaleString() always rounds the number to 10.0.

>> (9.95).toLocaleString('en-US', {minimumFractionDigits: 1, maximumFractionDigits: 1})
"10.0"
>> (9.95).toLocaleString('en-US', {minimumFractionDigits: 1, maximumFractionDigits: 1, roundingMode: "trunc"})
"10.0"

I want to use it for Deno but the question is about the javascript API not only about Deno. Am I doing something wrong?.

I'm using this in my script (9.95).toString().substring(0,3) because the rounding problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十六岁半 2025-02-16 14:13:39

我认为在 roundingmode ' ecmascript®2023国际化API规范。我认为没有一种方法可以使用 number.prototype.toleocalestring ,也不使用更高级的 intl.numberformat.prototype.format.format

但是,您可以使用 Math.trunc

>> Math.trunc(9.95 * 10) / 10
9.9

然后您可以将其转换为字符串,等等。

I see no mention of a roundingMode in ECMAScript® 2023 Language Specification nor in ECMAScript® 2023 Internationalization API Specification. I do not believe there is a way to do this with Number.prototype.toLocaleString nor with the more advanced Intl.NumberFormat.prototype.format.

You can however use some math with Math.trunc:

>> Math.trunc(9.95 * 10) / 10
9.9

Which you can then convert to a string, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文