带有 strlen 的 substr

发布于 2024-12-01 16:32:59 字数 328 浏览 2 评论 0原文

我有一个类似于 0003000 的金额,最后 2 位数字是十进制数。我想将 0003000 转换为 00030,00 (在最后 2 位数字前面插入小数点逗号)。 我尝试用子字符串来做到这一点。我用 strlen 获取字符串的长度,然后生成 -2,但它忽略了 -2。

这是一个例子,为什么它被忽略了?

substr($this->arrCheck['amountsum'],0,(strlen($this->arrCheck['amountsum']-2)))

I have an amount like 0003000, the last 2 digits are the decimal number. I want to transform 0003000 to 00030,00 (insert a decimal comma in front of the last 2 digits).
I tried to do this with substring. I take the length of the string with strlen and then I make -2, but it ignores the -2.

Here is an example, why is it being ignored?

substr($this->arrCheck['amountsum'],0,(strlen($this->arrCheck['amountsum']-2)))

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

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

发布评论

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

评论(7

踏雪无痕 2024-12-08 16:32:59

这是因为您的 -2 位于 strlen 函数中而不是外部:

strlen($this->arrCheck['amountsum']-2)

应该是:

strlen($this->arrCheck['amountsum'])-2

但总而言之,您不需要使用 strlen,因为 substr 接受负数作为 strlen 之前的字符数字符串结尾: PHP 手册

因此上面的整个代码可以替换为:

substr($this->arrCheck['amountsum'], 0, -2)

整个事情可以通过以下方式实现:

substr($this->arrCheck['amountsum'], 0, -2).','.substr($this->arrCheck['amountsum'], -2)

It's because your -2 is in the strlen function instead of outside it:

strlen($this->arrCheck['amountsum']-2)

Should be:

strlen($this->arrCheck['amountsum'])-2

But all in all you don't need to use strlen, since substr accepts a negative number as number of characters before the end of the string: PHP Manual

So your whole code above can be replace by:

substr($this->arrCheck['amountsum'], 0, -2)

And the whole thing can be achieved by:

substr($this->arrCheck['amountsum'], 0, -2).','.substr($this->arrCheck['amountsum'], -2)
友谊不毕业 2024-12-08 16:32:59

-2 之后有 strlen 右括号。尝试:

substr($this->arrCheck['amountsum'],0,(strlen($this->arrCheck['amountsum'])-2))

You have the strlen close bracket after the -2. Try:

substr($this->arrCheck['amountsum'],0,(strlen($this->arrCheck['amountsum'])-2))
花想c 2024-12-08 16:32:59

在这种情况下,您不需要 strlen(),请尝试:

substr($this->arrCheck['amountsum'], 0, -2) . ',' . substr($this->arrCheck['amountsum'], -2)

you don't need strlen() in this case, try:

substr($this->arrCheck['amountsum'], 0, -2) . ',' . substr($this->arrCheck['amountsum'], -2)
醉南桥 2024-12-08 16:32:59

您还可以执行以下操作:

echo substr_replace("0003000", ",", -2, 0); // output: 00030,00

echo number_format("0003000" / 100, 2, ',', ''); // output: 30,00

You can also do the folowing:

echo substr_replace("0003000", ",", -2, 0); // output: 00030,00

echo number_format("0003000" / 100, 2, ',', ''); // output: 30,00
红颜悴 2024-12-08 16:32:59

使用 number_format() 代替。这样就很容易使用了。

http://www.w3schools.com/php/func_string_number_format.asp

use number_format() instead. That will be easy to use.

http://www.w3schools.com/php/func_string_number_format.asp

2024-12-08 16:32:59

您可以使用 number_format

http://php.net/manual/en/function.number -format.php

或更正您的代码。

substr($this->arrCheck['amountsum'],0,(strlen($this->arrCheck['amountsum']) -2))

你不能将 -2 放入 strlen() 函数中

You can use number_format

http://php.net/manual/en/function.number-format.php

or correct your code.

substr($this->arrCheck['amountsum'],0,(strlen($this->arrCheck['amountsum']) -2))

you can't place -2 into strlen() function

伏妖词 2024-12-08 16:32:59

这边走 ?

substr($this->arrCheck['amountsum'], 0, -2).','.substr($this->arrCheck['amountsum'], -2);

this way ?

substr($this->arrCheck['amountsum'], 0, -2).','.substr($this->arrCheck['amountsum'], -2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文