12,953,491 members (59,980 online)
Rate this:
See more:
I am trying to divide a/b and show the result in decimal
for eg: i am dividing 10/3 and the result should come 3.3333 but mine project shows 3 only what can i do.
i am writing mine code as
`outputtextblock1.Text = Convert.ToString(Convert.ToInt32(Decimal.Parse(cf1.Text))/Convert.ToInt32(Decimal.Parse(cf2.Text)));`
Posted 26-Nov-12 3:29am
Updated 26-Nov-12 3:58am
v2

Rate this:

## Solution 4

When you divide two integers, you'll get only the integer part of that division.
The easiest way to change it is:
`decimal a = 10 / (decimal)3;`

You can also put an "m" after a number to indicate it's a decimal type. If you wanted to convert it to float or double, you use "f" instead.
Example:
`decimal a = 10 / 3m;`

You can also call Convert functions:

`decimal a = 10 / Convert.ToDecimal(3);`

In these cases it's only needed to cast only one of your values, as both variables are numeric.
In your code, on the other hand, you'll need to cast both as you can't divide a string.

Applying that to your code, it could be something like this:

`outputtextblock1.Text = (Convert.ToDecimal(cf1.Text) / Convert.ToDecimal(cf2.Text)).ToString()`

Or maybe like this:
`outputtextblock1.Text = (Decimal.Parse(cf1.Text) / Decimal.Parse(cf2.Text)).ToString();`

I hope this helps. :)
v4
Keshav Gyawali 26-Nov-12 10:48am

Thank you so much
it really works....
entilete 26-Nov-12 16:30pm

You're welcome :)
Rate this:

## Solution 2

You are converting both the dividend and divisor to `Int32` and then performing the division, hence a integer division is performed.
You may, for instance, remove the `Convert.ToInt32` calls.
Rate this:

## Solution 3

Hello,

You could do it like this:

```string a = "10";
string b = "3";

double result = double.Parse(a) / double.Parse(b);

outputtextblock1.Text = result.ToString();
```

Valery.

Top Experts
Last 24hrsThis month
 OriginalGriff 190 ppolymorphe 140 RickZeeland 65 Member 13197019 65 Dave Kreskowiak 40
 OriginalGriff 6,429 CHill60 3,490 Maciej Los 3,103 ppolymorphe 2,020 Jochen Arndt 1,975