Click here to Skip to main content
12,953,491 members (59,980 online)
Rate this:
Please Sign up or sign in to vote.
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
Rate this: bad
Please Sign up or sign in to vote.

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.
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. :)
Keshav Gyawali 26-Nov-12 10:48am
Thank you so much
it really works....
entilete 26-Nov-12 16:30pm
You're welcome :)
I added more examples that I forgot to add before.
Rate this: bad
Please Sign up or sign in to vote.

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: bad
Please Sign up or sign in to vote.

Solution 3


You could do it like this:

string a = "10";
string b = "3";
double result = double.Parse(a) / double.Parse(b);
outputtextblock1.Text = result.ToString();


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,429
CHill60 3,490
Maciej Los 3,103
ppolymorphe 2,020
Jochen Arndt 1,975

Advertise | Privacy | Mobile
Web01 | 2.8.170525.1 | Last Updated 26 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100