简体   繁体   中英

How to convert string to number in php without rounding?

I am retrieving some values stored as string which have up to 15 decimal places. I need to json_encode it and pass it on to javascript as numeric value. So I tried (float)$number , but the number gets rounded-off in this approach. How else can I convert it to number without any rounding?

You should use the GMP library in PHP whenever number precision is important. You may have to enable it inside of your php.ini settings. Search for...

;extension=php_gmp.dll

and change it to

extension=php_gmp.dll

and then you'll be able to use the GMP objects.

If you want to preserve those decimals: don't parse it. Any number converted to float is subject to precision loss . Can't you just print the variable as received in JS?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM