Rounding numbers (both ways)
-
Im in a C++ MFC visual studio 6.0 workspace. i have a float, which i need to round, both mathematically and string wise.. For example mathematically this; 0.306-> 0.31 0.99-> 1.00 and string wise; 0.12000000 -> 0.12 12.0000000 -> 12 Ive surfed around, but only found ones that dont remove unnecessary zeros, and dont work perfectly for all numbers.. The variable i have is a float, and im guessing its atof thats creating the extra zeros (which i have to use, or something equivalent) rounding should be to 2dp, ie allways x.xx any ideas? thanks!
/Johannes
-
Im in a C++ MFC visual studio 6.0 workspace. i have a float, which i need to round, both mathematically and string wise.. For example mathematically this; 0.306-> 0.31 0.99-> 1.00 and string wise; 0.12000000 -> 0.12 12.0000000 -> 12 Ive surfed around, but only found ones that dont remove unnecessary zeros, and dont work perfectly for all numbers.. The variable i have is a float, and im guessing its atof thats creating the extra zeros (which i have to use, or something equivalent) rounding should be to 2dp, ie allways x.xx any ideas? thanks!
/Johannes
Johpoke wrote:
For example mathematically this; 0.306-> 0.31 0.99-> 1.00
This is int integer = (int)(0.306 + 0.5);
Johpoke wrote:
and string wise; 0.12000000 -> 0.12 12.0000000 -> 12
Maybe boosts format library[^] can help you?
"We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation." -- Caius Petronius, Roman Consul, 66 A.D.
-
Im in a C++ MFC visual studio 6.0 workspace. i have a float, which i need to round, both mathematically and string wise.. For example mathematically this; 0.306-> 0.31 0.99-> 1.00 and string wise; 0.12000000 -> 0.12 12.0000000 -> 12 Ive surfed around, but only found ones that dont remove unnecessary zeros, and dont work perfectly for all numbers.. The variable i have is a float, and im guessing its atof thats creating the extra zeros (which i have to use, or something equivalent) rounding should be to 2dp, ie allways x.xx any ideas? thanks!
/Johannes
Johpoke wrote:
0.306-> 0.31 0.99-> 1.00
this is a matter of representation, so basically, printf() does it ! for the 2 questions btw, you can have a look here[^]
[VisualCalc][Flags Beginner's Guide] | [Forums Guidelines][My Best Advice]
-
Im in a C++ MFC visual studio 6.0 workspace. i have a float, which i need to round, both mathematically and string wise.. For example mathematically this; 0.306-> 0.31 0.99-> 1.00 and string wise; 0.12000000 -> 0.12 12.0000000 -> 12 Ive surfed around, but only found ones that dont remove unnecessary zeros, and dont work perfectly for all numbers.. The variable i have is a float, and im guessing its atof thats creating the extra zeros (which i have to use, or something equivalent) rounding should be to 2dp, ie allways x.xx any ideas? thanks!
/Johannes
There's also the
ceil()
andfloor()
functions. It's really just a matter of trying each suggestion until you find the one that does just what you need.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
Im in a C++ MFC visual studio 6.0 workspace. i have a float, which i need to round, both mathematically and string wise.. For example mathematically this; 0.306-> 0.31 0.99-> 1.00 and string wise; 0.12000000 -> 0.12 12.0000000 -> 12 Ive surfed around, but only found ones that dont remove unnecessary zeros, and dont work perfectly for all numbers.. The variable i have is a float, and im guessing its atof thats creating the extra zeros (which i have to use, or something equivalent) rounding should be to 2dp, ie allways x.xx any ideas? thanks!
/Johannes
Strings:
CString str; float f = 0.306; //float f = 12.0000000f; str.Format(_T("%.2g"), f); TRACE(_T("%s\n"), str);
Because I've used %g (instead of %f), 12.0 will be 12. Numerics: Take a look at this: http://www.codeproject.com/cpp/floatutils.asp[^]
- Dy
-
Strings:
CString str; float f = 0.306; //float f = 12.0000000f; str.Format(_T("%.2g"), f); TRACE(_T("%s\n"), str);
Because I've used %g (instead of %f), 12.0 will be 12. Numerics: Take a look at this: http://www.codeproject.com/cpp/floatutils.asp[^]
- Dy
-
Now, that seems good, but it does lots of weird things that i dont want, try setting f to, 299.99 or 31.2699 thanks!!
/Johannes
that's normal floating point behavior... know that their height storage capability is at the cost of the precision
[VisualCalc][Flags Beginner's Guide] | [Forums Guidelines][My Best Advice]
-
that's normal floating point behavior... know that their height storage capability is at the cost of the precision
[VisualCalc][Flags Beginner's Guide] | [Forums Guidelines][My Best Advice]
-
Im in a C++ MFC visual studio 6.0 workspace. i have a float, which i need to round, both mathematically and string wise.. For example mathematically this; 0.306-> 0.31 0.99-> 1.00 and string wise; 0.12000000 -> 0.12 12.0000000 -> 12 Ive surfed around, but only found ones that dont remove unnecessary zeros, and dont work perfectly for all numbers.. The variable i have is a float, and im guessing its atof thats creating the extra zeros (which i have to use, or something equivalent) rounding should be to 2dp, ie allways x.xx any ideas? thanks!
/Johannes
Well, that's not very "generic" (what happens when you need to round at a higher precision?).
double Rounder(double fVal, int nPrecision)
{
nPrecision = __max(__min(16, nPrecision), 1);
double fEpsilon = pow(10, nPrecision);
double fResult = ( floor(fVal * fEpsilon + 0.5) / fEpsilon);
}"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001