Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.12/5 (3 votes)
See more:
Dear Friends


i Am new to this c# programming and i ma soorry for asking silly question


i have two double variables and i am multiplying it the result should come in decimal

eg user enters qty and rate price should calculate and displaying grid

like user enters qty = 2 rate =3 but answer must show 6.00
Posted

There's nothing to it, you just need to cast the double to a decimal, here's how:

C#
double qty = 2;
double rate = 3;
double result = qty * rate;

// converting to decimal from double can be done by simply casting it to decimal :)
decimal decResult = (decimal)result ;

// This will create a string with precision of two digits after the dot, "6.00"
string formattedWithTwoDigitsAfterDot = decResult.ToString("#.##");

// do something with your result string
Console.WriteLine("The Decimal result is: " + formattedWithTwoDigitsAfterDot );




[Update - Added Examples /]
See examples on formatting a double ToString from MSDN:

C#
double value;

value = 123;
Console.WriteLine(value.ToString("00000"));
// Displays 00123

value = 1.2;
Console.WriteLine(value.ToString("0.00", CultureInfo.InvariantCulture));
// Displays 1.20
Console.WriteLine(value.ToString("00.00", CultureInfo.InvariantCulture));
// Displays 01.20
Console.WriteLine(value.ToString("00.00",
                  CultureInfo.CreateSpecificCulture("da-DK")));
// Displays 01,20

value = .56;
Console.WriteLine(value.ToString("0.0", CultureInfo.InvariantCulture));
// Displays 0.6

value = 1234567890;
Console.WriteLine(value.ToString("0,0", CultureInfo.InvariantCulture));
// Displays 1,234,567,890
Console.WriteLine(value.ToString("0,0",
                  CultureInfo.CreateSpecificCulture("el-GR")));
// Displays 1.234.567.890

value = 1234567890.123456;
Console.WriteLine(value.ToString("0,0.0", CultureInfo.InvariantCulture));
// Displays 1,234,567,890.1

value = 1234.567890;
Console.WriteLine(value.ToString("0,0.00", CultureInfo.InvariantCulture));
// Displays 1,234.57





Cheers,
Edo
 
Share this answer
 
v6
Comments
murkalkiran 10-Sep-13 2:36am    
Sorry Edo U r code is showing answer as only 6 but i need 6.00;
murkalkiran 10-Sep-13 2:37am    
hh
Joezer BH 10-Sep-13 2:40am    
solution updated.
I don't understand though why you chose to use Decimal??

why not just double all the way?
murkalkiran 10-Sep-13 2:48am    
ya i used double for all three variables but its showing only 6,
if i multiply qty=2.3 and rate= 1.5 the answer showing 3.45 its correct if give the value 2 and 3 it should show 6.00
Joezer BH 10-Sep-13 2:55am    
so you can keep using double for the result and then show what you like, in any precision,
See examples added on the above solution
Quote:
but answer must show 6.00

That has nothing to do with the numeric data type, that is number representation.
You may just use the Double.ToString("F2") method, see "Standard Numeric Format Strings" at MSDN[^].
 
Share this answer
 
You Can Follow this way....


C#
 qty=2;
rate=3;
double Ans;
textBox3.Text =string.Format("{0:0.00}",  Convert.ToDouble(textBox1.Text) * Convert.ToDouble(textBox2.Text)); 
 
Share this answer
 
v3
Comments
murkalkiran 10-Sep-13 2:44am    
sorry u r code also showing only 6 it should display 6.00
If you want your result in the format like 6.00 you don't have to use a decimal.

Just use double . To make sure only to display 2 digits after the . you could use

Math.Round

C#
public static double Round (
    double value,
    int digits
)
 
Share this answer
 
v2
Comments
phil.o 10-Sep-13 2:38am    
"If you want your result in the format like 6.00 you can't use a decimal."
Your assertion is false. Indeed you can. Didn't you mean "If you want your result in the format like 6.00 you don't have to use a decimal." instead?
Lauryx 10-Sep-13 2:44am    
I'm sorry, I think I need one more coffee.. Thanks
Joezer BH 10-Sep-13 2:45am    
5ed!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900