printf - php - using sprintf to add "+" sign removes decimals from formatted number -
i want show changes in stock index:
12 => +12.00 150.5 => +150.50 -30.2 => -30.20 -2.85193 => -2.85
i've got this:
sprintf("%+d", number_format(floatval($key), 2, '.', ','))
but it's stripping decimals formatted number , returning things +45
.
is there efficient way both + sign , decimals?
%d
integers, have use %f
floating point.
sprintf("%+.2f", $key);
unfortunately, can't commas this. wouldn't have worked original code, because %d
parses argument integer, , stop reading number when gets comma.
if need both sign , commas, can do:
($key >= 0 ? '+' : '') . number_format(floatval($key), 2, '.', ','))
Comments
Post a Comment