Reduce float number in c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

20th Aug 2017, 9:08 PM
peter
peter - avatar
2 Answers
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, ...
20th Aug 2017, 10:41 PM
StoneSmasher
StoneSmasher - avatar
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!
12th Sep 2017, 8:46 AM
peter
peter - avatar