473,395 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

rounding a float/double to nearest 1/10th

Any one have a better/simpler method for rounding a float to the nearest
1/10th? This is currently what I am using, and there must be a better
way, or perhaps a canned method that I am not aware of.

double z = atof(arg[1]);
z = z*100.0;
int zi = (int)floor((double)z);
int ri = zi%10;
zi -= ri;
zi += ( ri < 5 ) ? 0 : 10;
z = (double)zi/(double)100;

NOTE: originally the doubles were floats, but 1.5*100 = 149.9999 when
using floats, correct answer given when doubles used.

Any suggestions would be great thank.

~S

Jul 19 '05 #1
13 25897
Shea Martin wrote:
Any one have a better/simpler method for rounding a float to the nearest
1/10th? This is currently what I am using, and there must be a better
way, or perhaps a canned method that I am not aware of.

double z = atof(arg[1]);
z = z*100.0;
int zi = (int)floor((double)z);
int ri = zi%10;
zi -= ri;
zi += ( ri < 5 ) ? 0 : 10;
z = (double)zi/(double)100;

NOTE: originally the doubles were floats, but 1.5*100 = 149.9999 when
using floats, correct answer given when doubles used.

Any suggestions would be great thank.

~S


float z2 = atof(arg[1]);
z2 = (float)floor(z2*10+0.5)/10;

I thinks this is a lot better.

~S

Jul 19 '05 #2
> float z2 = atof(arg[1]);
z2 = (float)floor(z2*10+0.5)/10;


better

z2 = (float)(floor( z2 * 10 + 0.5 ) / 10)

So the value gets chopped to a float (if at all) after the division.
Jul 19 '05 #3
Shea Martin wrote:
Any one have a better/simpler method for rounding a float to the nearest
1/10th? This is currently what I am using, and there must be a better
way, or perhaps a canned method that I am not aware of.

double z = atof(arg[1]);
z = z*100.0;
int zi = (int)floor((double)z);
int ri = zi%10;
zi -= ri;
zi += ( ri < 5 ) ? 0 : 10;
z = (double)zi/(double)100;

NOTE: originally the doubles were floats, but 1.5*100 = 149.9999 when
using floats, correct answer given when doubles used.

z = 0.1 * round( z * 10.0 );

Jul 19 '05 #4
Gianni Mariani wrote:
Shea Martin wrote:
Any one have a better/simpler method for rounding a float to the
nearest 1/10th? This is currently what I am using, and there must be
a better way, or perhaps a canned method that I am not aware of.

double z = atof(arg[1]);
z = z*100.0;
int zi = (int)floor((double)z);
int ri = zi%10;
zi -= ri;
zi += ( ri < 5 ) ? 0 : 10;
z = (double)zi/(double)100;

NOTE: originally the doubles were floats, but 1.5*100 = 149.9999 when
using floats, correct answer given when doubles used.


z = 0.1 * round( z * 10.0 );

Which header do I need to get round? It does not seem to be in math.h
on Solaris 9.

Thanks,

~S

Jul 19 '05 #5

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bn********@dispatch.concentric.net...
[SNIP]
z = 0.1 * round( z * 10.0 );


Somehow I missed that round() was a standard function. I'd be happy if you
could point out where I can find it, so that I can get rid of my own
solution.

I usually use the following approach:

double Round( double Value, int Digits )
{
if( Value > 0.0 )
return ( (long)( Value * Faktor + 0.5 ) ) / pow( 10.0, Digits);

return ( (long)( Value * Faktor - 0.5 ) ) / pow( 10.0, Digits);
}

In case of common values for Digits one could use a table instead of
calculating the factor with pow() as it is a rather slow function.

Regards
Chris
Jul 19 '05 #6

"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message
news:bn**********@sunnews.cern.ch...

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bn********@dispatch.concentric.net... [SNIP] double Round( double Value, int Digits )
{
if( Value > 0.0 )
return ( (long)( Value * pow( 10.0, Digits) + 0.5 ) ) / pow( 10.0, Digits);

Sorry, this should of course be
if( Value > 0.0 )
return ( (long)( Value * pow( 10.0, Digits) + 0.5 ) ) / pow( 10.0,
Digits);

return ( (long)( Value * pow( 10.0, Digits) - 0.5 ) ) / pow( 10.0,
Digits);
}


It's obviously too early for me :-)

Chris
Jul 19 '05 #7

"Shea Martin" <sm*****@arcis.com> wrote in message news:fNCnb.9864$f7.536027@localhost...
z = 0.1 * round( z * 10.0 );

Which header do I need to get round? It does not seem to be in math.h
on Solaris 9.

Ain't no such function. Try nearbyint or rint.
Jul 19 '05 #8

"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message news:bn**********@sunnews.cern.ch...

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bn********@dispatch.concentric.net...
[SNIP]
z = 0.1 * round( z * 10.0 );


Somehow I missed that round() was a standard function. I'd be happy if you
could point out where I can find it, so that I can get rid of my own
solution.

Should be in math.h. It's a C function, but part of C99.
Jul 19 '05 #9

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m...

"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message

news:bn**********@sunnews.cern.ch...

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bn********@dispatch.concentric.net...
[SNIP]
z = 0.1 * round( z * 10.0 );


Somehow I missed that round() was a standard function. I'd be happy if you could point out where I can find it, so that I can get rid of my own
solution.

Should be in math.h. It's a C function, but part of C99.


Aah, thanks Ron. Didn't know that.

Cheers
Chris
Jul 19 '05 #10
Ron Natalie wrote:
"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message news:bn**********@sunnews.cern.ch...
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bn********@dispatch.concentric.net...
[SNIP]
z = 0.1 * round( z * 10.0 );


Somehow I missed that round() was a standard function. I'd be happy if you
could point out where I can find it, so that I can get rid of my own
solution.


Should be in math.h. It's a C function, but part of C99.

One more reason to upgrade our aging compilers (Sun WorkShop 5.0). :-)
~S

Jul 19 '05 #11
Shea Martin wrote:
Any one have a better/simpler method for rounding a float to the nearest
1/10th?


Don't know what you want to do with the result, but how about

char s[30];
sprintf(s,"%.1lf",z); // s is z rounded to nearest 0.1
z=atof(s);

sh************@computer.org (remove caps for e-mail)

Jul 19 '05 #12

"Shea Martin" <sm*****@arcis.com> wrote in message
news:fNCnb.9864$f7.536027@localhost...
Gianni Mariani wrote:
Shea Martin wrote:
Any one have a better/simpler method for rounding a float to the
nearest 1/10th? This is currently what I am using, and there must be
a better way, or perhaps a canned method that I am not aware of.

double z = atof(arg[1]);
z = z*100.0;
int zi = (int)floor((double)z);
int ri = zi%10;
zi -= ri;
zi += ( ri < 5 ) ? 0 : 10;
z = (double)zi/(double)100;

NOTE: originally the doubles were floats, but 1.5*100 = 149.9999 when
using floats, correct answer given when doubles used.


z = 0.1 * round( z * 10.0 );

Which header do I need to get round? It does not seem to be in math.h
on Solaris 9.

Thanks,

~S

----------------------------------------------
Personally I prefer a car.
Jul 19 '05 #13
"Steven C." <no****@xxx.com> wrote in message news:oW*****************@twister.socal.rr.com...
"Shea Martin" <sm*****@arcis.com> wrote in message
news:fNCnb.9864$f7.536027@localhost...
Gianni Mariani wrote:
Shea Martin wrote:
Any one have a better/simpler method for rounding a float to the
nearest 1/10th? This is currently what I am using, and there must be
a better way, or perhaps a canned method that I am not aware of.

double z = atof(arg[1]);
z = z*100.0;
int zi = (int)floor((double)z);
int ri = zi%10;
zi -= ri;
zi += ( ri < 5 ) ? 0 : 10;
z = (double)zi/(double)100;

NOTE: originally the doubles were floats, but 1.5*100 = 149.9999 when
using floats, correct answer given when doubles used.


z = 0.1 * round( z * 10.0 );

Which header do I need to get round? It does not seem to be in math.h
on Solaris 9.


You need a version of math.h that better conforms to C99. We offer
such a library, but it's an extra-cost item.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 19 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Marc Schellens | last post by:
My compiler warns about assignment to int form float/double. Will it nevertheless do what I expect? Are there caveeats/pits? (Apart from the range and the precision of course) What would be the...
26
by: Alexander Block | last post by:
Hello newsgroup, let's say I have a function like template<class Type> inline bool areEqual(const Type &a, const Type &b) { return ( a == b ); }
4
by: JKop | last post by:
Does the Standard specify any minimum range or minimum precision for the float, double and long double. I haven't found anything in the Standard about it. -JKop
3
by: Madan | last post by:
Hi all, I had problem regarding float/double arithmetic only with + and - operations, which gives inaccurate precisions. I would like to know how the arithmetic operations are internally handled...
13
by: kennethlou | last post by:
Hi, If in C a variable appears like X=10.000000, I can round it to zero decimal places. ie X=10? I then have to save the number into a variable. The method appears below:...
4
by: illegal.prime | last post by:
Hi all, I have a class that I would like to use to hide the data type I'm using to store decimal point numbers. Failing all else, I will just use object and do conversion/casts as necessary. But...
1
by: lindabaldwin | last post by:
Hi everyone, I have a code that I can use in Excel to round to the nearest 0.5, but I cannot get it to work in Access. When I try to execute it I get the following error message: "Compile error:...
1
by: veremue | last post by:
I want to store a float/double as String. I have an input file with a column with float values i.e. have the form 1.4E15. I want to store these values as Strings. Here is my code which is resulting...
3
by: Wander | last post by:
currently there are those functions is_float, is_double and is_real, which do exactly the same as far as i can see what surprises me is that $a = (float/double/real) 1; all work, while with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.