beric
V2EX  ›  问与答

PHP 中有没有办法让浮点型变量保留 0 的小数位

  •  
  •   beric · Apr 10, 2017 · 3398 views
    This topic created in 3507 days ago, the information mentioned may be changed or developed.
    比如做 JSON 输出时,想输出如:{"value":1.00 }如何能实现?
    用 number_format 这个函数的话会变成字符类型:{"value":"1.00"}
    强制转换成 float ,又会变成整数{"value":1 }
    5 replies  •  2017-04-10 23:01:30 +08:00
    cevincheung
    cevincheung
    Apr 10, 2017
    1
    1.00 就是 integer 啊。
    justseemore
    justseemore
    Apr 10, 2017
    2
    printf('%0.2f',1)
    sagaxu
    sagaxu
    Apr 10, 2017
    3
    没有意义,就算你输出 1.00 ,使用这个 json 的地方, decode 出来还是 1
    azh7138m
    azh7138m
    Apr 10, 2017 via Android
    4
    不用 float ,用整形,前端处理除 100 就好了
    wangjie
    wangjie
    Apr 10, 2017
    5
    json 就是为了承载数据啊, 1.00 和 1 是相等的呀,都是 number 类型,你要想保持 1.00 就保持 string 就好啦
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4308 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 06:52 · PVG 14:52 · LAX 22:52 · JFK 01:52
    ♥ Do have faith in what you're doing.