Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

truncate

Posted on 1998-10-18
9
Medium Priority
?
1,169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 3

Expert Comment

by:arnond
ID: 1175414
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
ID: 1175415
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
ID: 1175416
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 3

Expert Comment

by:arnond
ID: 1175417
leowlf,
did my suggestion help ?
would you like me to post an answer for this question ?
0
 

Author Comment

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

Accepted Solution

by:
arnond earned 100 total points
ID: 1175419
Here's my dummy answer.
Thanks.
0
 
LVL 1

Expert Comment

by:pibarale
ID: 24084984
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
ID: 24085285
> 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
ID: 24104023
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

688 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