Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does php realize rounding and does not retain decimals

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report