如何将浮点值(从JSON获取)转换为PHP中的字符串,保持“尾随零”?

如何将浮点值(从JSON获取)转换为PHP中的字符串,保持“尾随零”?

问题描述:

I've a JSON (i.e. a javascript object).

I have a page that get the content of the JSON (i.e. a set of fields,array and objects).

Some of these fields are float values. The number of DECIMAL DIGITS depends on the specific field.

My question is: how do I print the WHOLE number as it was a STRING"?

Example:

   12.35 --> print: 12.35 (but this is ok,a floatval() works!)
   12.0 --> print: 12.0 (this is NOT ok,because floatval() prints just "12")
   12.00 --> print: 12.00 (this is NOT ok,because floatval() prints just "12")

Of course I tried to print the value without casting(i.e. without using floatval() ). But is seems that the cast is done "implicity" by PHP.

And of course if there are not 0 (e.g. "12") no 0s should be printed

Thank you in advance for any tip.

我有一个JSON(即一个javascript对象)。 p>

I 有一个获取JSON内容的页面(即一组字段,数组和对象)。 p>

其中一些字段是 float strong>值。 DECIMAL DIGITS strong>的数量取决于特定字段。 p>

我的问题是:如何打印整个数字,因为它是STRING strong>“? p>

示例: p>

  12.35  - > print:12.35(但这没关系,floatval()有效 !)
 12.0  - >打印:12.0(这不好,因为floatval()打印只是“12”)
 12.00  - >打印:12.00(这不好,因为floatval()只打印 “12”)
  code>  pre> 
 
 

当然我试图打印值而不需要 strong>(即不使用floatval())。但似乎 强制转换 strong>由PHP完成“implicity” strong>。 p>

当然如果没有0(例如“12”) 不应打印0 strong> p>

提前感谢您的任何提示。 p> div>

Try with this code:

$a=(float)"12.00";
print number_format($a,2);