Get the App
SLTechnology News&Howtos  ›  Development  › 

How php converts a string to double and retains two decimal places

Shulou Source: shulou.com Published: 2022-05-31 19:52:50 09月14日 Update

Most people do not understand the knowledge points of this article "php how to convert the string to double type and retain two decimal places", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "php how to convert strings to double type and retain two decimal places" article.

Conversion and retention of two decimal places: 1, use the "floatval (string)" statement to convert the string to double type; 2, use the "number_format (double type value, 2)" or "sprintf (% .2f", double type value) "statement to retain two decimal places.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Php converts the string to double and retains two decimal places

You can see two parts:

Convert string to double type: use floatval ()

Two decimal places that retain the sprintf value: using the number_format () or sprintf () function

Number_format () can format numbers by grouping them in thousands.

The sprintf () function writes the formatted string to a variable.

Sample code:

Description:

When using the number_format () or sprintf () function to retain two decimal places, if the rounded value is greater than 5, it will be rounded.

The above is about the content of this article on "how php converts strings to double types and retains two decimal places". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Tags: Decimals characters strings types content functions decimal points articles formats knowledge articles statements rounding two code values variables most of them that is tutorials Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Apple macOS Redmi Shulou Technology