473,385 Members | 1,641 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,385 software developers and data experts.

rounding to the nearest nth digits

Hi

I googled, RTM, checked round, floor, ceil, setprecision for no avail.
I have a vector<doublewhich I need to round its elements to the nth
digit so that if n is 2 then
1.135 would be 1.14
0.9955 would be 1.00

thanks

Aug 8 '06 #1
11 8175

Gary Wessle wrote:
Hi

I googled, RTM, checked round, floor, ceil, setprecision for no avail.
I have a vector<doublewhich I need to round its elements to the nth
digit so that if n is 2 then
1.135 would be 1.14
0.9955 would be 1.00

thanks
I think if x = 1.135 and you want to round it to the 2nd (n=2) digit
(so it'll be 1.14) do this
x=round(x*(pow(10,n))/pow(10,n);

Aug 8 '06 #2
"Gary Wessle" <ph****@yahoo.comwrote in message
news:87************@yahoo.com...
: I googled, RTM, checked round, floor, ceil, setprecision for no avail.
: I have a vector<doublewhich I need to round its elements to the nth
: digit so that if n is 2 then
: 1.135 would be 1.14
: 0.9955 would be 1.00
For example:
double mul = pow(10,n);
for( ... i = .... )
v[i] = floor( v[i]*mul + 0.5 ) / mul;

hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Aug 8 '06 #3

"Ivan Vecerina" <IN*****************@ivan.vecerina.comwrote in message
news:88**************************@news.hispeed.ch. ..
"Gary Wessle" <ph****@yahoo.comwrote in message
news:87************@yahoo.com...
: I googled, RTM, checked round, floor, ceil, setprecision for no avail.
: I have a vector<doublewhich I need to round its elements to the nth
: digit so that if n is 2 then
: 1.135 would be 1.14
: 0.9955 would be 1.00
For example:
double mul = pow(10,n);
for( ... i = .... )
v[i] = floor( v[i]*mul + 0.5 ) / mul;
There's no guarantee that that will result in the value desired. Remember,
floating-point values are binary representations, and can only be exactly
represented for a small subset of specific values. Just like there's no way
to represent 1/3 exactly using decimals (it's .3333333... forever), there is
no way to represent many fractions using binary. So making this type of
calculation, where n can be anything, may result in values near to what you
want, but not exact.

The question is, where does the rounding need to occur? If it's just for
output, then the correct thing to do would probably be to format the output,
not to modify the stored value.

If you're talking about currency (such as dollars and cents) or some other
fixed-point system, then you might want to simply scale everything upwards
as needed. For example, instead of storing 1.135 as 1.14, you could scale
it up to 113.5, then round that to the integer 114. Then you can stick the
decimal where you want it when displaying or printing out the value.

If you've got a more complex (or generic) situation, then perhaps you could
explain it more fully and someone could suggest a solution for your
particular needs.

-Howard


Aug 8 '06 #4
Gary Wessle wrote:
I googled, RTM, checked round, floor, ceil, setprecision for no avail.
I have a vector<doublewhich I need to round its elements to the nth
digit so that if n is 2 then
1.135 would be 1.14
0.9955 would be 1.00
What for? What problem are you trying to solve by rounding?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 8 '06 #5
Gary Wessle <ph****@yahoo.comwrites:
Hi

I googled, RTM, checked round, floor, ceil, setprecision for no avail.
I have a vector<doublewhich I need to round its elements to the nth
digit so that if n is 2 then
1.135 would be 1.14
0.9955 would be 1.00

thanks
double x = 1.123;
double y = 10.215;
cout << setprecision(4) << x << " "
<< setprecision(4) << y << endl;

cout << setprecision(2) << x << " "
<< setprecision(2) << y << endl;

**************** outputs ****************
1.123 10.21
1.1 10

**************** desired ****************
1.1230 10.2150
1.12 10.22
Aug 8 '06 #6
Gary Wessle wrote:
Gary Wessle <ph****@yahoo.comwrites:
>Hi

I googled, RTM, checked round, floor, ceil, setprecision for no avail.
I have a vector<doublewhich I need to round its elements to the nth
digit so that if n is 2 then
1.135 would be 1.14
0.9955 would be 1.00

thanks

double x = 1.123;
double y = 10.215;
cout << setprecision(4) << x << " "
<< setprecision(4) << y << endl;

cout << setprecision(2) << x << " "
<< setprecision(2) << y << endl;

**************** outputs ****************
1.123 10.21
1.1 10

**************** desired ****************
1.1230 10.2150
1.12 10.22
This code dates from 1991. It was used to display
financial data on reports.

Use at your own risk...

#include <math.h>

/* round then trunc z at d decimal digits
* z; nbr to round
* d; dec digits to round/trunc to: 0 - 14
*/
double FixDec(double z, int d)
{
double m, f;

if(z == 0.0) return(z);

if(d < 0) d = 0;
if(d 14) d = 14; /* IEEE doubles max precision minus 1 */
/* comments show what would happen if z = 1234.5678 & d = 2 */
m = pow(10.0, (double)d); /* m: = 100.0 */
z *= m; /* z: 1234.5678 -123456.78 */
f = modf(z, &z); /* z: 123456.78 -123456.0 & f = 0.78 */
f *= 10.0; /* f: 0.78 -7.8 */
modf(f, &f); /* f: 7.8 -7.0 */
if((int)f 4)
z += 1.0; /* z: 123456.0 -123457.0 */

return(z / m); /* 123457.0 / 100.0 = 1234.57 */

}
Aug 8 '06 #7
"Victor Bazarov" <v.********@comAcast.netwrites:
Gary Wessle wrote:
>I googled, RTM, checked round, floor, ceil, setprecision for no avail.
I have a vector<doublewhich I need to round its elements to the nth
digit so that if n is 2 then
1.135 would be 1.14
0.9955 would be 1.00

What for? What problem are you trying to solve by rounding?
so that instead of printing 1.123, it prints 1.12 and instead of
10.123 it prints 10.12, pretty printing is what I am after.
Aug 8 '06 #8
Larry I Smith wrote:
Gary Wessle wrote:
>Gary Wessle <ph****@yahoo.comwrites:
>>Hi

I googled, RTM, checked round, floor, ceil, setprecision for no avail.
I have a vector<doublewhich I need to round its elements to the nth
digit so that if n is 2 then
1.135 would be 1.14
0.9955 would be 1.00

thanks
double x = 1.123;
double y = 10.215;
cout << setprecision(4) << x << " "
<< setprecision(4) << y << endl;

cout << setprecision(2) << x << " "
<< setprecision(2) << y << endl;

**************** outputs ****************
1.123 10.21
1.1 10

**************** desired ****************
1.1230 10.2150
1.12 10.22

This code dates from 1991. It was used to display
financial data on reports.

Use at your own risk...

#include <math.h>

/* round then trunc z at d decimal digits
* z; nbr to round
* d; dec digits to round/trunc to: 0 - 14
*/
double FixDec(double z, int d)
{
double m, f;

if(z == 0.0) return(z);

if(d < 0) d = 0;
if(d 14) d = 14; /* IEEE doubles max precision minus 1 */
/* comments show what would happen if z = 1234.5678 & d = 2 */
m = pow(10.0, (double)d); /* m: = 100.0 */
z *= m; /* z: 1234.5678 -123456.78 */
f = modf(z, &z); /* z: 123456.78 -123456.0 & f = 0.78 */
f *= 10.0; /* f: 0.78 -7.8 */
modf(f, &f); /* f: 7.8 -7.0 */
if((int)f 4)
z += 1.0; /* z: 123456.0 -123457.0 */

return(z / m); /* 123457.0 / 100.0 = 1234.57 */

}
I must note that the return value from FixDec() was then used in
an fprintf() call using a format string (something like "10.2lf").

Larry
Aug 8 '06 #9
Gary Wessle wrote:
"Victor Bazarov" <v.********@comAcast.netwrites:
>Gary Wessle wrote:
>>I googled, RTM, checked round, floor, ceil, setprecision for no
avail. I have a vector<doublewhich I need to round its elements
to the nth digit so that if n is 2 then
1.135 would be 1.14
0.9955 would be 1.00

What for? What problem are you trying to solve by rounding?

so that instead of printing 1.123, it prints 1.12 and instead of
10.123 it prints 10.12, pretty printing is what I am after.
Read about i/o manipulators, especially 'setprecision'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 9 '06 #10

Gary Wessle wrote:
Hi

I googled, RTM, checked round, floor, ceil, setprecision for no avail.
I have a vector<doublewhich I need to round its elements to the nth
digit so that if n is 2 then
1.135 would be 1.14
0.9955 would be 1.00

thanks
This is a right royal pain in the wossname. It seems that everybody
gets so carried away with significant figures and thinks there is no
good reason to use number of decimal places.

For your use in currencies though using reals is a very bad idea. You
should be using an integer type multiplied by some value (100 or 1000)
depending on the currency and the accuracy required.

Having said that though if you have old code that's already using
floats to store money I just hope it's for reporting purposes...

Here is what I came up with (f is a real, set dp to the number of
places):

int mag = int( std::log10( f ) );
if ( dp + mag < 1 )
ss << std::setprecision( dp + 1 ) << 0.0;
else
ss << std::setprecision( dp + mag + 1 ) << f;
I'm sure this has all sorts of interesting behaviour for extreme values
and numbers of places. I'm also not sure what happens if dp is set to a
negative value - haven't needed it and haven't thought it through.

In case people are wondering why on earth you'd want to do this sort of
thing consider lat/lon positions. Your GPS doesn't suddenly become more
accurate as you near the meridian.

0.123456W is the same accuracy as 100.123456W. If you output 0.123456
and then 100.124 then you're doing something very wrong. Reals are the
natural representation to use to store these numbers too.
K

Aug 9 '06 #11
Gary Wessle <ph****@yahoo.comwrote:
Gary Wessle <ph****@yahoo.comwrites:
>I googled, RTM, checked round, floor, ceil, setprecision for no avail.
I have a vector<doublewhich I need to round its elements to the nth
digit so that if n is 2 then
1.135 would be 1.14
0.9955 would be 1.00

thanks

double x = 1.123;
double y = 10.215;
cout << setprecision(4) << x << " "
<< setprecision(4) << y << endl;

cout << setprecision(2) << x << " "
<< setprecision(2) << y << endl;

**************** outputs ****************
1.123 10.21
1.1 10

**************** desired ****************
1.1230 10.2150
1.12 10.22
You want to add std::fixed to your output statement.
#include <iostream>
#include <iomanip>

int main()
{
double x = 1.123;
double y = 10.215;
std::cout << std::fixed << std::setprecision(4) << x << " "
<< std::setprecision(4) << y << '\n';

std::cout << std::setprecision(2) << x << " "
<< std::setprecision(2) << y << '\n';
}

/*

Output:
1.1230 10.2150
1.12 10.22

*/

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Aug 9 '06 #12

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

Similar topics

13
by: Shea Martin | last post by:
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...
3
by: b | last post by:
Hello all, we have a table that is part of our accounting package that stores decimals as such: 123.45678. However the reporting standards with the accounting system display that as 123.45 note...
14
by: calan | last post by:
Does anyone have a function that will round a number to 0 or .5? I have a form where I'm entering a number in inches. I need to round it to the nearest 1/2 inch (onChange). The split will be...
6
by: Jeff Boes | last post by:
(asked last week on .questions, no response) Can anyone explain why this happens? (under 7.4.1) select '2004-05-27 09:00:00.500001-04' :: timestamp(0) ; timestamp ---------------------...
3
by: mlafarlett | last post by:
In the example below, the 'convert.ToDecimal' removes the trailing 5. I'd like to get the number, in tact, moved to the decimal variable. Any help would be greatly appreciated. Dim dcml As...
29
by: Marco | last post by:
Hello, I have : float f = 36.09999999; When I do : char cf; sprintf(cf,"%0.03lf", f); I get : 36.100
4
by: AMDRIT | last post by:
Given: ?System.Math.Round(161.5D,0) 162.0 ?System.Math.Round(162.5D,0) 162.0 ?System.Math.Round(163.5D,0) 164.0 In either C# '05 or VB.Net '05, can anyone explain the inconsistancy? Is
5
by: Spoon | last post by:
Hello everyone, I don't understand how the lrint() function works. long lrint(double x); The function returns the nearest long integer to x, consistent with the current rounding mode. It...
206
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.