Get the App
SLTechnology News&Howtos  ›  Development  › 

How does php realize rounding and does not retain decimals

Shulou Source: shulou.com Published: 2022-06-01 19:53:28 09月30日 Update

This article will explain in detail how to realize the rounding of php and does not retain decimals, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

The method of realizing rounding by php does not retain decimals: 1, rounding directly through intval function, discarding decimals; 2, rounding through round; 3, rounding up through ceil; 4, rounding down through floor.

This article operating environment: Windows7 system, PHP7.1 version, DELL G3 computer

How does php realize rounding and not keeping decimals?

There are four common methods for PHP to get an integer function:

Intval (): rounding directly, discarding decimals, keeping integers

Round (): round off

Ceil (): round up and add 1 to decimals

Floor (): round down.

Number_format (): the function formats numbers by grouping them in thousands.

Intval () integer conversion function

Int intval (mixed $var [, int $base = 10])

Returns the integer value of the variable var by using the specified decimal base conversion (default is decimal). Intval () cannot be used for object, otherwise an E_NOTICE error will be generated and 1 will be returned.

Echo intval (42); / / 42echo intval (4.2); / / 4echo intval ('42'); / / 42

Round () function

Round (number,precision,mode)

Round up floating-point numbers.

Echo round (42.12123); / / 42echo round (42.62123); / / 43echo round (42.12123, 0); / / 42echo round (42.12123, 2); / / 42.12echo round (4212123,-2); / / 421212300

One method of ceil () function

Returns the next integer not less than value, and value takes one place if it has a decimal part.

Echo ceil (42.12123) / / 43echo ceil (42.62123); / / 43

Floor () function de-tailing method

Returns the next integer not greater than value, rounding off the fractional portion of value.

Echo floor (42.12123) / / 42echo floor (42.62123); / / 42

Number_format () function

The number_format () function formats numbers by grouping thousands of bits.

Number_format (number,decimals,decimalpoint,separator)

Number: required. The number to format. If no other parameter is set, the number is formatted with no decimal point and a comma (,) as the thousand separator.

Decimals: optional. How many decimals are specified. If this parameter is set, the number is formatted with a period (.) as the decimal point.

Decimalpoint: optional. Specifies a string to use as a decimal point.

Separator: optional. Specifies the string to use as a thousand-bit delimiter. Only the first character of the parameter is used. For example, "xxx" only outputs "x".

Echo number_format ("1000000"); / / 1000000echo number_format ("1000000", 2); / / 1000000.00echo number_format ("1000000", 2, "."); / / 1.000.0000000echo number_format ("1000000", 2, "*", "."); / / 1.000.000*00echo number_format ("1000000", 2, "."); / / 1000000.00

On how to achieve php rounding and do not retain decimals to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Decimals functions numbers formats integers rounding parameters characters decimal points rounding content delimiters strings articles methods more knowledge articles parts enter one Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Redmi macOS Shulou Technology vpn