How to display just first 2 decimals unequal to 0

前端 未结 4 1477
甜味超标
甜味超标 2021-01-18 10:52

How can I display the number with just the 2 not=zero decimals?

Example:

For 0.00045578 I want 0.00045 and for 1.0000533535 I want 1.000053

4条回答
  •  孤城傲影
    2021-01-18 11:23

    My solution would be to convert the number to a string. Search for the ".", then count zeroes till you find a non-zero digit, then take two digits.

    It's not an elegant solution, but I think it will give you consistent results.

提交回复
热议问题