PHP decimal Two bits reserved

Source: Internet
Author: User
This article mainly introduces the content is about PHP two decimal places, has a certain reference value, now share to everyone, the need for friends can refer to

Recently in doing statistics this piece of content, contact about the number of data more,

Three functions are used to keep the decimal number after N bits, and then a simple introduction to three functions:

1, Number_format

Echo Number_format ("5000000"). " <br> "; Echo Number_format (" 5000000 ", 2)." <br> "; Echo Number_format (" 5000000 ", 2,", ",". ");

This function is mainly implemented by: Thousands of bits are grouped into formatted data.

Output results

5,000,000
5,000,000.00
5.000.000,00

2, sprintf

$num = 123; $data =sprintf ("%.2f", $num); Echo $data;

The result is:

123.00

3, round


$num 1 = 123.1267$data1 = Round ($num 1, 2), $num 2 = 123$data2 = Round ($num 2, 2); Echo $data 1. " <br> "Echo $data 2


The result is:

123.13

123

This function will not automatically help you if the decimal point is less than two bits.

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.