PHP: keep the last decimal point and keep the last decimal point. _ PHP Tutorial

Source: Internet
Author: User
PHP: keep the last decimal point and keep the last decimal place rounded. $ Total1225.49.5; $ totalfloor ($ total * 0.95) * 10) 10; $ totalsprintf (%. 1f, (float) $ total); echo $ total ;? Output: 1225.4php: several methods to retain the last decimal point


$ Total = 1225.49.5;

$ Total = floor ($ total * 0.95) * 10)/10;

$ Total = sprintf ('%. 1f', (float) $ total );

Echo $ total;


?>

Output result: 1225.4


Several methods for retaining the last decimal point in php


$ Total = number_format (2/3, 1); // 1 indicates that the last digit of the decimal point is retained and is rounded
Echo $ total ."
";


$ Total_1 = sprintf ("%. 1f", 2/3); // 1f indicates the last decimal point
Echo $ total_1 ."
";


$ Total_2 = printf ("%. 1f", 2/3 );
Echo $ total_2 ."
";


?>

Cost = 1225.49.5; $ total = floor ($ total * 0.95) * 10)/10; $ total = sprintf ('%. 1f', (float) $ total); echo $ total;?> Output result: 1225.4 php retains the first digit after the decimal point...

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.