What's the best way to get the fractional part of a float in PHP?

前端 未结 10 941
不思量自难忘°
不思量自难忘° 2020-11-30 05:45

How would you find the fractional part of a floating point number in PHP?

For example, if I have the value 1.25, I want to return 0.25.

10条回答
  •  孤独总比滥情好
    2020-11-30 05:52

    However, if you are dealing with something like perlin noise or another graphical representation, the solution which was accepted is correct. It will give you the fractional part from the lower number.

    i.e:

    • .25 : 0 is integer below, fractional part is .25
    • -.25 : -1 is integer below, fractional part is .75

    With the other solutions, you will repeat 0 as integer below, and worse, you will get reversed fractional values for all negative numbers.

提交回复
热议问题