Reputation: 213
I wish to utilize som sort of combination of %f and %g in my fprintf. I wish to remove trailing zeros and use 6 decimal points if there are 6 decimals that are non zero. %g removes zeros but here precision does not affect the number of decimal points. What to do?
Upvotes: 2
Views: 4205
Reputation: 14108
You can use %.<n>g
, where n defines maximal number of digits to be used.
>> fprintf('%.6g\n', 4087.145678);
4087.15
>> fprintf('%.6g\n', 45.2);
45.2
Upvotes: 0
Reputation: 4388
So if this is the behaviour you want:
0.123456789 -> 0.123457 (rounded up at the 6th decimal place)
999.123456789 -> 999.123457 (six decimal places, regardless of the number of significant figures)
1.123000000 -> 1.123 (remove trailing zeros)
then you can use %f:
fprintf('%.6f', number);
Upvotes: 3