从数字字符串中删除千位分隔符

发布于 2024-08-16 10:06:47 字数 219 浏览 5 评论 0原文

我有一个变量 $totalprice,其编号为 1,072.00

如果我使用 int($totalprice)number_format($totalprice),它会输出 1.00

我该如何解决这个问题?我想要 1072.00

I have a variable $totalprice with number 1,072.00.

If I use int($totalprice) or number_format($totalprice), it outputs 1.00.

How can I solve this problem? I want 1072.00.

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

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

发布评论

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

评论(3

一张白纸 2024-08-23 10:06:47
$totalprice = (float)str_replace(',', '', $totalprice)

这将为您提供数字的正常浮点表示形式。然后,您可以对其应用 number_format 函数以获得所需的输出。

也许你应该重新考虑你对数字的处理。首先,数字是如何获得 1,072.00 格式的?在输出到浏览器之前,不应对其进行任何格式化,因此该数字应始终采用 1072.00 格式。

$totalprice = (float)str_replace(',', '', $totalprice)

That will get you a normal float representation of the number. You can then apply the number_format function to that to get the desired output.

Perhaps you should rethink your handling of numbers. How in the first place the number gets the format 1,072.00? You shouldn't do any formatting to it until outputting to the browser, so the number should always be in the 1072.00 format.

不即不离 2024-08-23 10:06:47

您遇到的问题与语言环境有关。为了避免有关货币格式的问题,我建议您使用

string money_format ( string $format , float $number )

通过使用此功能,您可以根据所需的格式格式化数字。您可以在此处阅读有关此函数以及要使用的格式类型的更多信息: money_format

Problem you are having is about locales. To avoid issue about money formatting i would suggesT you to use

string money_format ( string $format , float $number )

By using this function you can format the number according to desired format. you can read more about this function and what formatting type to use here: money_format

睫毛溺水了 2024-08-23 10:06:47

这不是一个错误,因为您的号码是从左到第一个无效字符()解析的,

所以 1,072.00 实际上是 1,072.00 ,只是 粗体当您尝试将字符串转换为数字时,将解析 部分。

It is not a bug ,because your number is parsed from left till first invalid character (the ,)

so 1,072.00 is really 1,072.00 , and just the bold section is parsed when you are trying to convert the string to number.

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