Solved

truncate

Posted on 1998-10-18
9
1,161 Views
Last Modified: 2008-03-10
VC5

Is there function that i can truncate
a double of value 3.333333333333333......
to a fewer number of decimal point?

double D = 3.33333333..... // return by some function
double MyNewD = truncate_D_to_2_decimal_point(D) // ???



0
Comment
Question by:leowlf
9 Comments
 
LVL 3

Expert Comment

by:arnond
Comment Utility
one way is to do:

double truncate (double D,int n)
{
  long multiplied;
  double to_return;

  multiplied = (D*pow(10,n));         // save only the first n digits after the decimal point . (as a long integer).
  to_return  = (multiplied / (pow(10,n));  // return to the trtunced (original ) number.
 
  return (to_return);
}
 
for example:
in truncate (0.123456789,5) you'll get:
multiplied = 12345
to_return = 0.12345
 
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
That would work, but you have to understand that because of the limitations of the floating point format, not all decimal numbers will be expressible, thus when you truncate the number, you may see the last digit or two change in value.  This is not problem with arnond's method, this is a limitation of the floating point system.  For this reason, most programmer's don't truncate the value at all.  They may limit the number of digits that print, but will try to retain full precision internally.
0
 
LVL 3

Expert Comment

by:arnond
Comment Utility
nietod is right, you can limit the number of digits you print using the format specifications of your favorite output function. for example: in printf() you should use "%.pf".
the 'p' stands for precision, i.e.: how many decimal places to display.

0
 
LVL 3

Expert Comment

by:arnond
Comment Utility
leowlf,
did my suggestion help ?
would you like me to post an answer for this question ?
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:leowlf
Comment Utility
arnond,
Your suggestion is good enough for me,
please post an answer.
Thank you.
0
 
LVL 3

Accepted Solution

by:
arnond earned 50 total points
Comment Utility
Here's my dummy answer.
Thanks.
0
 
LVL 1

Expert Comment

by:pibarale
Comment Utility
Hi Arnod,
I am using C language because I can not use C++ due to some desing issue.
how do we proceed if the double has this value
UINT s = 10;
int n = 16;
double dx = (double)(s/100.0);
here is dx = 0.10000000000000001
The above logic does not work.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
> you have to understand that because of the limitations of the floating point format, not all decimal numbers will be expressible, thus when you truncate the number, you may see the last digit or two change in value.
0
 
LVL 1

Expert Comment

by:pibarale
Comment Utility
Thanks OZO. This is also my understanding.
One concern we are use C code in such a huge platform like Win32 (using VS 2008 compiler). Can this irreguarity be fix by the vendor in near future?
With this I stop with satisfaction.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now