Page view counter

decimal or double multiplication ??

Last post 06-24-2009 11:25 AM by crtjr. 6 replies.

Sort Posts:

  • decimal or double multiplication ??

    02-27-2004, 3:49 PM
    • Loading...
    • yanywg
    • Joined on 06-20-2003, 11:04 AM
    • Posts 41
    • Points 205
    I have a problem as follows :

    ---code
    decimal Number1;
    decimal Number2;
    Number1 = 6.43;
    Number2 = Number1 * 0.05;

    --end of code

    I got the build error :

    Operator '*' cannot be applied to operands of type 'decimal' and 'double';

    please help !

    YL

  • Re: decimal or double multiplication ??

    02-27-2004, 5:34 PM
    • Loading...
    • yanywg
    • Joined on 06-20-2003, 11:04 AM
    • Posts 41
    • Points 205
    I figured out that I had to add 'M' as suffix to the decimal so the compiler could recognize it as DECIMAL instead of DOUBLE, like this: 0.05m. odd!

    But the issue still there, if I have a variable to hold the decimal number, what can I do ?
    for exampe: the variable name is Rate, can I do this : Ratem ? NO !!!.. what can I do ?

    Please help !!

  • Re: decimal or double multiplication ??

    02-29-2004, 3:18 PM
    • Loading...
    • Roscoe Brooks
    • Joined on 02-29-2004, 1:55 AM
    • Posts 1
    • Points 5
    In an expression with two operands, both of type decimal, the return will be of type decimal. This is true regardless if these operands are both literals suffixed with "M", or variables declared as type Decimal, or one of each.

    In the example below, three multiplication statements are executed. Note that although these statements differ in syntax, they are semantically similiar: they all multiply the decimal values 6.43 and 0.05 and return the result as a decimal type. So the answer to your question about the Rate variable is: just declare it a decimal type and you're set - no "M" required.

    // Sample C# code ...
    decimal Number1;
    decimal Number2;
    decimal Number3;
    decimal Number4;
    decimal Rate;

    // First operand is decimal variable. Second operand is decimal literal.
    Number1 = 6.43M;
    Number2 = Number1 * 0.05M;
    Console.WriteLine(Number2);

    // Both operands are decimal variables.
    Rate = 0.05M;
    Number3 = Number1 * Rate;
    Console.WriteLine(Number3);

    // Both operands are decimal literals.
    Number4 = 6.43M * 0.05M;
    Console.WriteLine(Number4);
  • Re: decimal or double multiplication ??

    03-02-2004, 5:09 PM
    • Loading...
    • yanywg
    • Joined on 06-20-2003, 11:04 AM
    • Posts 41
    • Points 205
    Thank you very much !
    My question is: if I have a piece of value passed through a method call. The type of this value could be string or double. How could I convert it to decimal ? I tried, but no success.
    Is there a way we can do it ?


    Thanks,
    YL
  • Re: decimal or double multiplication ??

    03-02-2004, 5:19 PM
    • Loading...
    • yanywg
    • Joined on 06-20-2003, 11:04 AM
    • Posts 41
    • Points 205
    Thanks, I figured it out !
  • Re: decimal or double multiplication ??

    06-22-2009, 5:11 AM
    • Loading...
    • dopoto
    • Joined on 06-22-2009, 9:07 AM
    • Posts 1
    • Points 2

    Way to give back to the other users...  You only use the forum to ask for free assistance, but once you find your answer you can't be bothered to share it with other people that go through your thread...

  • Re: decimal or double multiplication ??

    06-24-2009, 11:25 AM
    • Loading...
    • crtjr
    • Joined on 06-10-2008, 11:01 AM
    • Baltimore, MD
    • Posts 25
    • Points 19

    yanywg:
    Thanks, I figured it out !
     

     

     

    Dude, what did you do to figure it out? You wanna share?

    Clarence
Page 1 of 1 (7 items)