PHP show only significant (non-zero) decimals

前端 未结 11 1257
借酒劲吻你
借酒劲吻你 2021-01-07 19:32

In PHP (using built-in functions) I\'d like to convert/format a number with decimal, so that only the non-zero decimals show. However, another requirement of mine is that if

11条回答
  •  借酒劲吻你
    2021-01-07 19:46

    Out of the box that isn't possible because you have two different ways of treating the fragment of your floats. You'll first have to determine how many non-zero numbers there are in your fragment and then act accordingly with sprintf.

     " . sprintf("%.{$decimals}f", $number);
    
        echo "
    "; }

提交回复
热议问题