Reputation: 11984
I am using sprintf
function for formatting values. When i say values means both integer and decimal values.But when i use this function it rounds if decimal values are formatted(Not always).Can anyone say the reason behind this.I am using the following code.
echo sprintf("%010d",(1142.87 * 100))."\n"; //displays wrong value
echo str_pad((1142.87 * 100), 10, '0', STR_PAD_LEFT); //displays correct value
What is my need is to format a number into 10 digits.The second one works fine for me.
Upvotes: 0
Views: 1014
Reputation: 3813
The rounding is probably happening in the conversion to a float. Instead of d
you can use the parameter s
which treats the input as a string, and the number will print correctly:
echo sprintf("%010d",(1142.87 * 100))."\n"; //displays wrong value
echo sprintf("%010s",(1142.87 * 100))."\n"; //displays correct value
Upvotes: 3