How php converts a string to a number and retains two decimal places
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!