decimal or double multiplication ??

Rate It (1)

Last post 03-02-2004 5:19 PM by yanywg. 4 replies.

Sort Posts:

  • decimal or double multiplication ??

    02-27-2004, 3:49 PM
    • Member
      205 point Member
    • yanywg
    • Member since 06-20-2003, 7:04 AM
    • Posts 41
    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
    • Member
      205 point Member
    • yanywg
    • Member since 06-20-2003, 7:04 AM
    • Posts 41
    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
    Answer
    • Member
      15 point Member
    • Roscoe Brooks
    • Member since 02-29-2004, 1:55 AM
    • Posts 1
    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
    • Member
      205 point Member
    • yanywg
    • Member since 06-20-2003, 7:04 AM
    • Posts 41
    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
    • Member
      205 point Member
    • yanywg
    • Member since 06-20-2003, 7:04 AM
    • Posts 41
    Thanks, I figured it out !
Page 1 of 1 (5 items)