CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Junior Member
Join Date
Mar 2012
Posts
5

## I need help

I'm trying to multiply a numericUpDown input.Here is my code so far:

double pi = Math.PI;
decimal number1 = numericUpDown1.Value;
int RadiusToCircumference = number1 * 2 * pi;

Dont think im stupid that i didnt figure this out yet. just started programming yesterday.

2. Elite Member
Join Date
Jun 2008
Posts
2,477

## Re: I need help

No one thinks you are stupid, but you need to tell us exactly what is not working. What is wrong exactly? The only problem I see is this line:

Code:
`int RadiusToCircumference = number1 * 2 * pi;`
So that will truncate the result to an integer. Perhaps you wanted it that way, but I don't know because you didn't explain what the actual problem is.

3. Junior Member
Join Date
Mar 2012
Location
Nigeria
Posts
15

## Re: I need help

I think the problem with your code is in the statement below:

int RadiusToCircumference = number1 * 2 * pi;

There are rules for working and converting between C# types. See my recommendation below:

double pi = Math.PI;
decimal number1 = Convert.ToInt32(textBox1.Text);
double RadiusToCircumference = (double)number1 * 2 * pi;
MessageBox.Show("The circumference is " + RadiusToCircumference);

These code should work based on these assumptions:

- For your own code I have assumed you are using C# 4.0 so I've done same.

- Here's what I think, if these varaible numericUpDown1.Value is of type double then that line of code should not be a problem else you'd need to explicitly cast(i.e. force a conversion from whatever type it is to type decimal) to do so just add (decimal) in front of that variable. Because I
have little idea about your numericUpDown1.Value, I initialized number1 variable from a textbox input.

As for the statement:
int RadiusToCircumference = number1 * 2 * pi;
observed that I've forced a conversion using a cast operator i.e. (double).

***********I hope these makes a good contribution to your understanding of C#***********

4. ## Re: I need help

It's best (numerically) to delay casting as late as possible:

[double]
double circum = (double)(radius * 2) * Math.Pi;
[/double]

In this case, it makes no difference, but it might if you were doing something more complicated.

Actually, an explicit cast is not required here. int * double will be automatically promoted to double.

5. Junior Member
Join Date
Mar 2012
Posts
5

## Re: I need help

Originally Posted by chillaxzino
I think the problem with your code is in the statement below:

int RadiusToCircumference = number1 * 2 * pi;

There are rules for working and converting between C# types. See my recommendation below:

double pi = Math.PI;
decimal number1 = Convert.ToInt32(textBox1.Text);
double RadiusToCircumference = (double)number1 * 2 * pi;
MessageBox.Show("The circumference is " + RadiusToCircumference);

These code should work based on these assumptions:

- For your own code I have assumed you are using C# 4.0 so I've done same.

- Here's what I think, if these varaible numericUpDown1.Value is of type double then that line of code should not be a problem else you'd need to explicitly cast(i.e. force a conversion from whatever type it is to type decimal) to do so just add (decimal) in front of that variable. Because I
have little idea about your numericUpDown1.Value, I initialized number1 variable from a textbox input.

As for the statement:
int RadiusToCircumference = number1 * 2 * pi;
observed that I've forced a conversion using a cast operator i.e. (double).

***********I hope these makes a good contribution to your understanding of C#***********
Thanks for the help. I will test it out tonight.

6. Junior Member
Join Date
Mar 2012
Posts
5

## Re: I need help

the problem has something to do with not being able to multiply a decimal variable. And numericUpDowns cannot be int.

by the way.I use visual c# express to build the program.
Last edited by yoda493; March 15th, 2012 at 10:05 AM.

7. Junior Member
Join Date
Mar 2012
Location
Nigeria
Posts
15

## Re: I need help

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•