PHP String到Float [英] PHP String to Float

查看:408
本文介绍了PHP String到Float的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不熟悉PHP,并有一个快速的问题。

I am not familiar with PHP at all and had a quick question.

我有2个变量@pricePerUnit和@invoicedUnits。以下是将这些值设置为值的代码:

I have 2 variables @pricePerUnit and @invoicedUnits. Here's the code that is setting these to values:

$InvoicedUnits = ((string) $InvoiceLineItem->InvoicedUnits);
$pricePerUnit = ((string) $InvoiceLineItem->PricePerUnit);

如果我输出这个,我得到正确的值。

If I output this, I get the correct values. Lets say 5000 invoiced units and 1.00 for price.

现在,我需要显示总支出金额。当我把这两个一起乘法它不工作(如预期,这是字符串)。

Now, I need to show the total amount spent. When I multiply these two together it doesn't work (as expected, these are strings).

但我没有线索如何解析/转换/转换变量在PHP 。

But I have no clue how to parse/cast/convert variables in PHP.

我应该怎么办?

推荐答案

$rootbeer = (float) $InvoicedUnits;

应该为你做。请查看类型 - 杂耍。您还应阅读字符串转换为数字

Should do it for you. Check out Type-Juggling. You should also read String conversion to Numbers.

这篇关于PHP String到Float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆