PHP keeps several tutorial instances after the decimal point

Source: Internet
Author: User
Tags float number sprintf
The content of this section:
PHP retains several digits after the decimal point

Example:

<?php$n = "10.6789";//One, retain 2 decimal points and round//Use round () method Echo round ($n, 2); echo "<br>";//Use Number_format () method Echo Number_format ($n, 2); echo "<br>"//Use sprintf () method Echo sprintf ("%.2f", $n); echo "<br>";

Second, retain 2 decimal places, but do not round

Echo ((int) ($n *100))/100;

Attached, PHP takes a few decimal points after the function number_format a simple example.

String Number_format (float number [, int decimals [, String Dec_point, String thousands_sep])

Code:

< $short _pi = "3.14159", $my _pi = Number_format ($short _pi, 2); Echo $my _pi. " \ n ";   3.14$foo = 850017.9021; Www.jbxue.com$new_foo = Number_format ($foo, 3, ".", ""), Echo $new _foo. " \ n ";  850 017.902?>

Description
The Number_format function is used to format the number of numbers.
The second function, decimals, is used to keep the numbers behind several.

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.