Click to See Complete Forum and Search --> : Truncating a double value - REPRISE
capitolc
May 1st, 2007, 01:32 PM
Yes an old thread, but just need advise on this topic.
What I would like to do, is truncate all the number after the decimal point WITHOUT rounding. I see that the Int and Fix commands comes from the VisualBasic.dll, but is there another way?
I see the use of controlling the floating decimals by formatting a string using N or F, but this again seems to round numbers. I even tried to use .ToString("N0") to show zero decimal points, but my number was rounded up (as with "F0").
I just would like to truncate my number if at all possible without adding the VisualBasic.DLL into my project. Possible?
TheCPUWizard
May 1st, 2007, 01:45 PM
Combine the two techniques. First use (int)((x) * 1?)/ 1??? this will get you "close", From there you should be able to tolerate the rounging of a string.format....
capitolc
May 1st, 2007, 06:00 PM
Ok, so I've been trying to figure out why the Int and Fix functions were not working in my program. I added the reference Microsoft.VisualBasic (runtime) DLL, and also included the USING MICROSOFT.VISUALBASIC namespace inside my program. So when I tried to use the Fix or Int functions, I got an error about it not being declared in the namespace or class.
Ok, fine, I'll try something else.
Still trying to truncate decimals from my number, I changed my value to a string (both using .ToString() and Convert.ToString() ). After, I try to use the TrimEnd() method to chop the string starting at the '.'. I test it by showing my string variable to make sure, and it ends up not trimming a thing and kept the entire decimal number. I know I used TrimEnd before and it worked, so why not this time?!?
Here is a function I created to trim away the decimals. Since I am working with a windows application, I ran the debugger and used the Console line to see what was going out as I stepped through the function.
public int Truncate(double a)
{
string line = a.ToString();
line = line.TrimEnd('.'); // also tried line.TrimEnd('.');
Console.WriteLine(line);
return Convert.ToInt32(line);
}
What could I be doing wrong?
zips
May 2nd, 2007, 10:35 AM
Has everyone forgotten about the tools provided?Double MyRounded = Math.Round(d, 2);
TheCPUWizard
May 2nd, 2007, 10:36 AM
Has everyone forgotten about the tools provided?Double MyRounded = Math.Round(d, 2);
Except that the request is to TRUNCATE, not round.... :rolleyes:
zips
May 2nd, 2007, 01:27 PM
CPU, it's true that in the original post jamespetts uses the word 'truncate'. However the target example he gave is in fact rounding, not truncating. And, much of the stuff in between (i.e. adding 0.5) addresses rounding, not truncating.
TheCPUWizard
May 2nd, 2007, 01:35 PM
Zips,
Yes it has gotten confusing, as floating point issues often do....
It has been my read that truncation of "intended" values occur. (Also discussed in other threads).
The issue is if you deal with floating point numbers that can not be represented exacltly, then you get small errors. If you then deo a truncation, you can "go the wrong way". For example "truncating" 0.1 after 1 decimal poit yields 0!!!!
dcell59
May 2nd, 2007, 02:47 PM
What could I be doing wrong?
TrimEnd() doesn't work the way you are thinking. You specify the characters to remove, not where to start from. If you have the string "Stuff..." and you do a TrimEnd('.') on it, you get "Stuff", but if you have the string "a.b.c" and perform the same operation, you get "a.b.c".
What you can do is to use LastIndexOf('.') to get the location of the rightmost '.', and use Substring() to extract the string up to that point.
capitolc
May 2nd, 2007, 04:31 PM
Originally, my problem was truncating the decimals from a given number WITHOUT rounding. After some time to think about it and some work-a-rounds, I came up with a quite simple way.
I created a method that takes a single number, converts it to a string, then cuts the string in half at the decimal point. The method simply returns the first half of the string as an integer.
public int Truncate(double a)
{
string[] line = Convert.ToString(a).Split('.');
return Convert.ToInt32(line[0]);
}
For my usage, this serves the purpose. Now that I look at this hours after coming up with this, I see that the return value may not work for a huge number. You may have to adjust to handle all cases. (I already have multiple inputs to count for int/double/decimal/float data types)
cjard
May 2nd, 2007, 07:35 PM
How do I truncate a double value to two decimal places, such that the number:
0.569657200248912
would become:
0.57?
Can I jsut point out that the operation you request is not a truncate, it is a round..
0.569657200248912 trunced to 2 dp would be 0.56
You can achieve this by many means... SImplest is to convert it to a string as you show it, and lop it off then:
.ToString(".00")
Or you can multiply it by 10^X where X is the dp to trunc to, then Math.Truncate() it, then divide it again..
cjard
May 2nd, 2007, 07:38 PM
For my usage, this serves the purpose.
So would Math.Truncate(), assuming it started life as a number before you made it a string ;)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.