PHP浮点转换失败,返回1

PHP浮点转换失败,返回1,php,laravel,Php,Laravel,我有一个函数,它使用number\u格式(值,2)返回值1725.00。因此,现在当我将其转换为float时,它给出了1,与int,intValue,floatValue相同。甚至我尝试用100乘以int值,但它给出的错误是一个格式不正确的数值。有人知道这里出了什么问题吗 $balance = (float) currentBalance($user_id); // currentBalance gives a value of 1725, but (float) gives makes the

我有一个函数,它使用
number\u格式(值,2)
返回值
1725.00
。因此,现在当我将其转换为
float
时,它给出了
1
,与
int
intValue
floatValue
相同。甚至我尝试用100乘以
int
值,但它给出的错误是
一个格式不正确的数值。有人知道这里出了什么问题吗

$balance = (float) currentBalance($user_id); // currentBalance gives a value of 1725, but (float) gives makes the value 1.
print_r($balance); die; //gives 1.

我使用的是
PHP7.0+
Laravel5.8
您的问题是
number\u格式
返回一个字符串,其中插入了逗号,表示千个分隔符,因此函数的返回值是
1725.00
。当您尝试将其转换为浮点时,PHP会将其转换为逗号,并表示这不再是一个数字,因此返回
1

如果需要由
currentBalance
返回格式化字符串,最好使用

$balance = (float)str_replace(',', '', currentBalance($user_id));

否则,将对
number\u format
的调用替换为对
round
的调用,以便
currentBalance
返回一个数值。

您的问题是
number\u format
返回一个字符串,其中插入了逗号,用于千个分隔符,因此函数的返回值是
1725.00
。当您尝试将其转换为浮点时,PHP会将其转换为逗号,并表示这不再是一个数字,因此返回
1

如果需要由
currentBalance
返回格式化字符串,最好使用

$balance = (float)str_replace(',', '', currentBalance($user_id));

否则,,将对
number\u format
的调用替换为对
round
的调用,这样
currentBalance
将返回一个数值。

我使用的
round
的可能重复的可能重复,并且它会给出
1
@Rob13您将调用更改为
number\u format
内部
当前余额
?这应该奏效。如果没有,我需要查看
currentBalance
的代码才能理解原因。不管怎样,我在回答中建议的使用str_replace将起作用。是的,很抱歉它起作用了,我以前理解了一些不同的东西。所以,我再次阅读了你的评论,这次我明白了。你的回答很有道理。非常感谢。@Rob13不用担心-我很高兴能帮上忙。我也使用了
,它给出了
1
@Rob13您将呼叫更改为
number\u格式
内部
currentBalance
?这应该奏效。如果没有,我需要查看
currentBalance
的代码才能理解原因。不管怎样,我在回答中建议的使用str_replace将起作用。是的,很抱歉它起作用了,我以前理解了一些不同的东西。所以,我再次阅读了你的评论,这次我明白了。你的回答很有道理。非常感谢。@Rob13别担心,我很高兴能帮上忙。