Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-01 18:39:08 10月02日 Update

This article introduces the knowledge of "how php converts strings into numbers and retains two decimal places". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Php converts a string into a number and retains two decimal places: 1, with the number_format () function, syntax "number_format ($str, 2)"; 2, with floatval () and round () function, syntax "round (floatval ($str))".

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

Php converts a string to a number and retains two decimal places

1. Use the number_format () function

The number_format () function is used to convert strings to numbers. It returns the formatted number on success, otherwise it gives E_WARNING on failure.

2. Use the floatval () + round () function

The floatval () function converts a string to a floating-point number, while the round () function rounds a floating-point number, leaving two decimal places.

That's all for "how php converts strings into numbers and retains two decimal places". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Functions characters strings numbers decimals content more points knowledge grammar utility success rounding achievement and then function dilemma reality situation tutorial Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Tech Info Redmi Shulou Information MySQL