0
Reduce float number in c++
i want divide two random number. the results in float would be like this=0.0000027567 . i want reduce float number to be less like =0.02. how change results now ? i will be appreciated answer me
2 Antworten
0
include the <cmath> module and use std::round to round your float to an int. If you want to leave some decimals do it the following way:
float a = 0.032728;
float rounded = std::round(a *100.0f) / 100.0f;
// with the factor 100 you convert your a to 3.2728. std::round rounds it to 3.0 and the '/' 100 converts it back to 0.03. For 3 decimals use 1000, for 4 decimals 10000, ...
0
thanks for you advise . its smart way to reduce float number but it seems you should know float number . i have dynamic float number with unpredictable decimal points so i chnage my code to avoid this numbers. thanks!