🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

8 total messages Started by Tomas Alvier Thu, 24 Feb 2000 00:00
Rounding off a double precision variable
#4137
Author: Tomas Alvier
Date: Thu, 24 Feb 2000 00:00
12 lines
1481 bytes
Is there a function for rounding of a double precision variable to a chosen
number of decimals?

Thanks in advance,
Tomas Alvier


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]
Re: Rounding off a double precision variable
#4138
Author: Thomas Krog Christensen
Date: Thu, 24 Feb 2000 00:00
17 lines
1684 bytes
"Tomas Alvier" <tal@nfis.no> wrote in message
news:C5Ns4.5456$kz6.102045@news1.online.no...
> Is there a function for rounding of a double precision variable to a
chosen
> number of decimals?

double void RoundOff(double x,int noDecimal){
    int f=x*pow(10,noDecimal);
    return floor(x*f+0.5)/f;
}


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]
Re: Rounding off a double precision variable
#4139
Author: Thomas Krog Christensen
Date: Fri, 25 Feb 2000 00:00
20 lines
1768 bytes
Ron Natalie wrote:
>Of course computationally that will do it, but the real question is
>what the original poster really wanted.  If he wanted the value only
>"DISPLAYED" to a certain number of decimal places, this won't accomplish
>it.

then you can use:

char buffer[100];
double v=5.457;
sprintf(buffer,"%.2f",v);

(but I don't think this is the answer to the original qustion)


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]
Re: Rounding off a double precision variable
#4140
Author: Ron Natalie
Date: Fri, 25 Feb 2000 00:00
25 lines
1965 bytes

Thomas Krog Christensen wrote:
> 
> "Tomas Alvier" <tal@nfis.no> wrote in message
> news:C5Ns4.5456$kz6.102045@news1.online.no...
> > Is there a function for rounding of a double precision variable to a
> chosen
> > number of decimals?
> 
> double void RoundOff(double x,int noDecimal){
>     int f=x*pow(10,noDecimal);
>     return floor(x*f+0.5)/f;
> }
>
Of course computationally that will do it, but the real question is
what the original poster really wanted.  If he wanted the value only
"DISPLAYED" to a certain number of decimal places, this won't accomplish
it.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]
fix
#4141
Author: Thomas Krog Christensen
Date: Fri, 25 Feb 2000 00:00
13 lines
1477 bytes
double void RoundOff(double x,int noDecimal){
    int f=pow(10,noDecimal);
   return floor(x*f+0.5)/f;
}



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]
Re: Rounding off a double precision variable
#4134
Author: Paul Meyerholtz
Date: Thu, 23 Mar 2000 00:00
30 lines
2133 bytes
Thomas Krog Christensen wrote:
> 
> "Tomas Alvier" <tal@nfis.no> wrote in message
> news:C5Ns4.5456$kz6.102045@news1.online.no...
> > Is there a function for rounding of a double precision variable to a
> chosen
> > number of decimals?
> 
> double void RoundOff(double x,int noDecimal){
>     int f=x*pow(10,noDecimal);
>     return floor(x*f+0.5)/f;
> }

A question about the function floor() from a newbie.  If I'm trying to
round off doubles the following seems to work:

double x;
....   x assigned some number ....
int rounded = int(x +.5);

Is this compiler dependent?  Don't C++ programs truncate when converting
from double to int?  

Paul

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]
Re: Rounding off a double precision variable
#4135
Author: Thomas Krog Christensen
Date: Thu, 23 Mar 2000 00:00
12 lines
1686 bytes
> Is this compiler dependent?  Don't C++ programs truncate when converting
> from double to int?

Yes they do. But if you want to truncate from double to double I think it's
faster to use floor.(if not there won't be any reason to use floor)


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]
Re: Rounding off a double precision variable
#4136
Author: Kaspar Daugaard
Date: Fri, 24 Mar 2000 00:00
23 lines
1925 bytes
Paul Meyerholtz wrote:
> A question about the function floor() from a newbie.  If I'm trying to
> round off doubles the following seems to work:
> 
> double x;
> ....   x assigned some number ....
> int rounded = int(x +.5);
> 
> Is this compiler dependent?  Don't C++ programs truncate when converting
> from double to int?


You should note that using floor() is different from truncating when the
value is negative, e.g. floor(-2.4) is -3 while int(-2.4) is -2.

- Kaspar

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads