Answer the question
In order to leave comments, you need to log in
How to properly format float to string?
The task is to convert a float to a string with rounding and with a given precision,
for example 2:
2.150000->2.15
2.000000->2
2.004000->2
2.005000->2.01
1140.000000->1140 Using
standard means with printf:
%.2f is always a fixed number (2 ) decimal places - not suitable
%.2g - seems to be suitable, but sometimes numbers with epsilon appear, for example, instead of 1140 -> 1.1e+02
Answer the question
In order to leave comments, you need to log in
Tell the whole problem. Perhaps you are trying to use floating point numbers where fixed point numbers are needed.
This is the quantity field. It may be fractional. On a mobile device, due to the small screen, I want integers to be shown without a part after the decimal point. It is also assumed that the maximum number of numbers after the decimal point will be 2. But, if suddenly this number increases in the future, then we would like rounding to occur.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question