Multiplying Decimals

  • Thread starter Thread starter Kuldeep
  • Start date Start date
K

Kuldeep

Hi All,

I have this piece of code shown below:

decimal ft = Convert.ToDecimal(txtft.Text);

decimal inch = Convert.ToDecimal(txtin.Text);

decimal metre = ((ft * 12) + inch) * 0.0254;

When I run this, I get the follwing error:

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

How do I get this done?

Thanks,

Kuldeep
 
Hi All,

I have this piece of code shown below:

decimal ft = Convert.ToDecimal(txtft.Text);

decimal inch = Convert.ToDecimal(txtin.Text);

decimal metre = ((ft * 12) + inch) * 0.0254;

When I run this, I get the follwing error:

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

How do I get this done?

Multiply by decimals instead:

decimal metre = ((ft * 12) + inch) * 0.0254m;

Note the "m" at the end of 0.0254 to force the literal to be a decimal
instead of a double.

Jon
 
Decimal.Multiply
Decimal.Add
Decimal.Negate
Decimal.Divide

Oops! I missed out on them I guess.

Kuldeep
 
Decimal.Multiply
Decimal.Add
Decimal.Negate
Decimal.Divide

Oops! I missed out on them I guess.

You don't need to start using explicit method calls. Just fix the
constant and all should be well.

Jon
 
Kuldeep said:
Decimal.Multiply
Decimal.Add
Decimal.Negate
Decimal.Divide

Oops! I missed out on them I guess.

yes, but you can still perform the operation the way Jon S said you could,
which is more readable IMO

in any case, this still won't work:

decimal metre = Decimal.Mutiply((ft * 12) + inch, 0.0254);

you need
decimal metre = Decimal.Mutiply((ft * 12) + inch, 0.0254m);
 
I wish the compiler/editor would fix this stuff for us (with our
confirmation). Sure, it won't catch/fix everything, but instead of simply
reporting a compile error, it could for example list the errors as it
currently does, but also have a small QuickFix button for each entry it can
guess at a fix. If a QuickFix button fixes the last error, start a
re-compile. For example, the compiler could easily figure the one below out
and suggest adding an "m". Also the line "a = b" is almost certainly
missing a semicolon so make that the option. It could even suggest changing
"sw.WriteLn" to "sw.WriteLine" etc. Agreed it won't always be correct (then
don't click the error's quickfix button), but it would be a great addition
(IMHO).

Hilton
 
Hilton said:
I wish the compiler/editor would fix this stuff for us (with our
confirmation). Sure, it won't catch/fix everything, but instead of simply
reporting a compile error, it could for example list the errors as it
currently does, but also have a small QuickFix button for each entry it can
guess at a fix. If a QuickFix button fixes the last error, start a
re-compile. For example, the compiler could easily figure the one below out
and suggest adding an "m". Also the line "a = b" is almost certainly
missing a semicolon so make that the option. It could even suggest changing
"sw.WriteLn" to "sw.WriteLine" etc. Agreed it won't always be correct (then
don't click the error's quickfix button), but it would be a great addition
(IMHO).

Well, I use quick fix a lot in Eclipse, but not for this sort of error
- more for things like adding imports etc. Personally I don't think
this sort of thing would make me much more productive - it's not the
kind of thing I run into often.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top