PHP floating point display and convert to string

Source: Internet
Author: User
You may think it is very easy to convert a floating point number (float) into a string in PHP, but common methods hide serious bugs. because PHP has a very unreasonable way to process floating-point numbers, and the precision will be lost. after research, it is actually a problem for PHP to display floating point numbers... "> <LINKhref =" http://www.php100.com//statics

You may think it is very easy to convert a floating point number (float) into a string in PHP, but common methods hide serious bugs. because PHP has a very unreasonable way to process floating-point numbers, and the precision will be lost. after research, it is actually a BUG in the display of floating point numbers in PHP. PHP built-in echo, var_dump, json_encode, string concatenation and other functions (commands) have problems when displaying floating point numbers, resulting in loss of precision.

$ A = 1315537636.338467;

Printf ("% f", $ a); echo "\ n ";

Echo $ a. "\ n ";

Echo $ a; echo "\ n ";

?>

Result

1315537636.338467

1315537636.3385

1315537636.3385

That is to say, it is impossible to convert a floating point number to a string or display it in PHP's best way. you must use printf/sprintf to convert a floating point number to a string.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.