hello people
I am trying to get what is after the decimal place in any number
for example 3.99939204820498, i want to get 99939204820498
i am aware of the % (mod) when i divide, but what about when i multiply
1458/365.25 = 3.9995646 (what ever the result is)
i wan tto then get the 0.9995646
lets say i multiply the 0.9995646 by 12 = 11.90545654 (some number)
now i want to get the 0.90545654 how do i do this in C# 2.0
i can use strings to split it using the . but is there a better way

Getting what is after the Decimal place, in any number
DamsDev2007
And about "number - (int)number". What does it do
You number is, e.g. 123.456. When it's converted to System.Int32 by (int) casting, only integer part is left (that is 123). And 123.456 - 123 is exactly what you need. You know, in the school they define the decimal part of the number as the difference between itself and its integer part.
phylyp
Hi,
The modulus operator having the symbol percent (%) is used to get the remainder of a division; we cannot use that operator to truncate a floting point number into whole number.
In addition to casting double to get the integer part of a double number, you could also use the Math.Truncate method:
double pi = Math.PI;
double piInt = Math.Truncate(pi);
double subnum = pi - piInt;
Using the above procedure, we can rewrite the NumberDecimalParser like this:
private double NumberDecimalParser(double input)
{
double truncatedInput = Math.Truncate(input);
double result = input - truncatedInput;
// This will extract only the numbers after
// the decimal places into a string
string subnumInString = result.ToString();
int decimalLocation = subnumInString.IndexOf('.') + 1;
if (decimalLocation > 1)
{
subnumInString = subnumInString.Remove(0, decimalLocation);
}
return double.Parse(subnumInString);
}
Best Regards,
-chris
TheOddMan
I don't know if I am overstepping my bounds, but there is a great feature in VS2K5 that allows people (like me) to play with the language: the Immediate window. To best understand the ways rudimentary functions behave start a program that only runs a breakpoint. Once the program stops, open the immediate window and start typing your code away. If you desire to see how a specific function works, use it many times in many different ways. That was how I obtained a grasp of these math functions (actually I used MatLab, but math functions are the same in any language). Also, if you are doing any heavy math stuff and don't want to write a library of math features, MathNet on sourceforge is a great program.
Benfield
Stefan Hinterscheid
You can get the subnumber of a float by subtracting it from its whole number:
double pi = 3.1415926; // This is your number
double subnum = pi - (int)pi; // Subtract it from the whole number
The result stored in subnum variable is 0.1415926, all you left to do now is a process to remove "0." from the begining of your number.
Regards,
-chris
AndyMauer
Please explain to me the () thingy thanks :)
ian
private
double NumberDecimalParser ( double number ){
double result= 0.0; string TempStr = number.ToString (); char[] Delim = { '.' }; string[] TempStrArry = TempStr.Split (Delim[0]); if (TempStr!="0"){
TempStr =
"0." + TempStrArry[1].ToString ();}
try{
result =
Convert.ToDouble ( TempStr );}
catch (Exception e){
MessageBox.Show ( e.ToString () );}
return result;}
but i see your way, and i dont understand this bit
double subnum = pi - (int)pi; // Subtract it from the whole number
What does (int)pi do why the brackets before pi why not after it or why not int (pi) can you explain to me, this is new to me as i am beginning C# programming
David V
but why did we not use the Modulus % to get the remainder
i mean would mod (%) give me this too
23423.2342/131.12 = number.decimals
if i used the mod (%) would it give me the decimals
JawKnee
Hi,
I used direct casting to cast variable from type double to integer, this it done this way:
(int)pi; // The (int) means I'm casting the variable pi to type int.
You can also use the convert class in place of direct casting, to make the code easier to read:
Convert.ToInt32(pi); // Same as the above
Both of the two methods, direct casting or by using the Convert class, is to cast the double to integer in order to remove the decimal places:
(int)pi; // returns 3
Convert.ToInt32(pi);
The subtraction looks like this:
double subnum = 3.1415926 - 3; // This returns 0.1415926
Hope this helps,
-chris