PHP money string conversion to integer error

后端 未结 9 638
小蘑菇
小蘑菇 2021-01-13 21:43

I have a small financial application with PHP as the front end and MySQL as the back end. I have ancient prejudices, and I store money values in MySQL as an integer of cents

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-13 22:23

    There's no point in storing money as integer if you enter it through a floating point operation (no pun intended). If you want to convert from string to int and be consistent with your "prejudice" you can simply use string functions.

    You can use an arbitrary precision library to divide by 10 (they handle numbers internally as strings), e.g. bcdiv() or gmp_div_q(), but of course, you could have also used it from the beginning for all the math.

    Or you can use plain string functions:

    
    

提交回复
热议问题