Question about sprintf decimal truncation

Source: Internet
Author: User
Problem: keep one decimal point, not rounded. if it is an integer, add 0. many people say sprintf (& #039; %. 1f & #039;, $ str);, but there is still a problem with the obtained result. Finally, this solution is used: echosprintf (& #039; %. 1f & #039;, floor ($ str); {code ...} isn't it about rounding it out? Why... problem: keep 1 decimal place, not rounded up, and add 0 to an integer

Many people use it.sprintf('%.1f',$str);But the result is still incorrect,
Finally, this solution is used:echo sprintf('%.1f', floor($str));


  

Isn't it about rounding it out? Why is it rounded to 5, but not to 6;
Other functionsnumber_format($str)It's all in step 5. Why?

Reply content:

Problem: keep 1 decimal point, not rounded, and add 0 to an integer.

Many people use it.sprintf('%.1f',$str);But the result is still incorrect,
Finally, this solution is used:echo sprintf('%.1f', floor($str));


  

Isn't it about rounding it out? Why is it rounded to 5, but not to 6;
Other functionsnumber_format($str)It's all in step 5. Why?

I tested it myself. the visual test is about the accuracy of floating point representation.


  
Reference

Linux floating point rounding or five or six (the representation of floating point numbers is related)

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.