Precise display decimal
When displaying decimal in the code, the number of decimal digits that are displayed are usually required to meet business needs. Rather than showing too high accuracy (such as 15 digits), it is better to trim the output to two more beautiful decimals.
In order to achieve this goal, we can use various variants of the
method of decimal variables. This method allows us to specify a format string to control the output format.
.ToString()
Consider the following format string variants:
: This format deletes the front guide zero and displays two decimals. For example, if is equal to 0.5m, this format will return
"#.##"
: This format also shows two decimals, but if the value is less than 1, it always contains the front guide. Therefore, if is 0.5m, it will return decimalVar
. "0.5"
"0.##"
decimalVar
The above is the detailed content of How Can I Control the Precision of Decimal Output in My Code?. For more information, please follow other related articles on the PHP Chinese website!