Getting what is after the Decimal place, in any number

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



Answer this question

Getting what is after the Decimal place, in any number

  • mandokev

    okay, i have managed to make a method


    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


  • DRK12345

    I Try to use Math.IEEERemainder - it should do what you want, and % is only for integer values, if I'm not mistaken (I'm a VB-guy, and I don't know C# very well).

    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.


  • xudeutsch

     Avada Kedavra wrote:
    yeah that helps alot,
    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


    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



  • Tamar

    yeah that helps alot,
    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


  • SHG

    Hi,

    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

  • skcheng

    Hello World,

    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

  • danb2005

     Avada Kedavra wrote:
    okay, i have managed to make a method

    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


    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


  • Saravana

    Second thing, what is the use of % then woudnt it give us the remaining of a division too like double subnum = pi - (int)pi;   // Subtract it from the whole number or no

    Please explain to me the () thingy thanks :)


  • Getting what is after the Decimal place, in any number