473,408 Members | 1,729 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Multiplying Decimals

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


Oct 25 '07 #1
6 18045
On Oct 25, 8:12 am, "Kuldeep" <kuldeep.vijayku...@hotmail.comwrote:
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

Oct 25 '07 #2
Decimal.Multiply
Decimal.Add
Decimal.Negate
Decimal.Divide

Oops! I missed out on them I guess.

Kuldeep
"Kuldeep" <ku****************@hotmail.comwrote in message
news:eI**************@TK2MSFTNGP02.phx.gbl...
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


Oct 25 '07 #3
On Oct 25, 10:16 am, "Kuldeep" <kuldeep.vijayku...@hotmail.comwrote:
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

Oct 25 '07 #4
Liz

"Kuldeep" <ku****************@hotmail.comwrote in message
news:Oe**************@TK2MSFTNGP02.phx.gbl...
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);

Kuldeep
"Kuldeep" <ku****************@hotmail.comwrote in message
news:eI**************@TK2MSFTNGP02.phx.gbl...
>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



Oct 25 '07 #5
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
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@e9g2000prf.googlegro ups.com...
On Oct 25, 8:12 am, "Kuldeep" <kuldeep.vijayku...@hotmail.comwrote:
>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

Dec 13 '07 #6
Hilton <no****@nospam.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 13 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Oswald | last post by:
Hey, how can I set the number of decimals? Example: 5 decimals 2.12345 2 decimals 2.12 I've found something about NumberFormat in the API, but without an example I can't solve this...
2
by: westjon64 | last post by:
i am fairly new to c++, i need to know how to make it so you can input decimals into a file using the cin >> a command instead of just being able to input whole numbers and also how to make it so...
2
by: k | last post by:
I have aproblem when multiplying 2 float 638.9 * 382.8 should = 244570.92 results giving 244570.922 both numbers are float variables , tried using double to...
6
by: Donal McWeeney | last post by:
Hi, Is there a way to specify on the predefined format strings like P and N that you want displayed all the decimals in the number... for example 3 will display 3 2.1 will display 2.1...
13
by: vsts2007 | last post by:
hi, how can I change the number of decimals appeared in access reports... I put a formula in query and its giving three to four decimals in the output of report. I want to change the number of...
12
by: CNiall | last post by:
I am very new to Python (I started learning it just yesterday), but I have encountered a problem. I want to make a simple script that calculates the n-th root of a given number (e.g. 4th root of...
0
by: Edwin.Madari | last post by:
for nth square root: use math.sqrt n times for example ... num = math.sqrt(num) ... 5.0 all comparisons work fine for arbitrary floating point numbers... For readability print them with...
0
by: Tommy Nordgren | last post by:
On 3 aug 2008, at 17.16, Edwin.Madari@VerizonWireless.com wrote: Ehum. The OP wants to compute the nth root ( not the nth square root) ------ What is a woman that you forsake her, and the...
8
by: =?Utf-8?B?a2FyaW0=?= | last post by:
Hello All, why is this code is not showing the result in this format: 0.00 and showing it as only 0 Private Sub btn1_Click Debug.Print(Format$(Rnd() * 100, "0.00")) Dim d As Double =...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.