PHP cast float to string is different from cast to int

前端 未结 2 1392
执念已碎
执念已碎 2021-01-07 09:16

Something you would not expect but need to be aware of when you\'re dealing with floating point numbers in php



        
2条回答
  •  既然无缘
    2021-01-07 10:10

    You're casting $i (3287) to a string and then to an int, so the result stays 3287. If you cast $i to an int you'll get 3286, and then if you cast it to a string you'll have what you want.

    Try echo (string) (int) $i

提交回复
热议问题