乘以双变量 [英] Multiplication of to double variable
问题描述
亲爱的朋友们
i我对这个c#编程很新,我可以用来问愚蠢的问题
i有两个双变量,我将它乘以结果应该是十进制的
例如用户输入数量和价格应该计算和显示网格
用户输入qty = 2 rate = 3但是答案必须显示6.00
没有什么可以做的,你只需要将双精度值转换为小数,这是如何:
double qty = 2 ;
double rate = 3 ;
double result = qty * rate;
// 从double转换为十进制可以通过简单地将其转换为十进制来完成:)
decimal decResult =( decimal )结果;
// 这将在点后创建一个精度为两位数的字符串,6.00
string formattedWithTwoDigitsAfterDot = decResult.ToString( ###。跨度>);
// 对结果字符串执行某些操作
控制台。 WriteLine( 十进制结果为: + formattedWithTwoDigitsAfterDot);
[更新 - 已添加示例/]
请参阅格式化示例来自MSDN的双ToString:
double 值跨度>;
value = 123 ;
Console.WriteLine( value .ToString( 00000\" 跨度>));
// 显示00123
value = 1 。 2 ;
Console.WriteLine( value .ToString( 0.00,CultureInfo.InvariantCulture));
// 显示1.20
Console.WriteLine( value .ToString( 00.00,CultureInfo.InvariantCulture));
// 显示01.20
Console.WriteLine( value .ToString( 00.00,
CultureInfo。 CreateSpecificCulture( da-DK)));
// 显示01,20
value =。 56 ;
Console.WriteLine( value .ToString( 0.0,CultureInfo.InvariantCulture));
// 显示0.6
value = 1234567890 ;
Console.WriteLine( value .ToString( 0,0,CultureInfo.InvariantCulture));
// 显示1,234,567,890
Console.WriteLine( value .ToString( 0,0,
CultureInfo.CreateSpecificCulture( el-GR)));
// 显示1.234.567.890
value = 1234567890 。 123456 ;
Console.WriteLine( value .ToString( 0,0.0,CultureInfo.InvariantCulture));
// 显示1,234,567,890.1
value = 1234 。 567890 ;
Console.WriteLine( value .ToString( 0,0.00,CultureInfo.InvariantCulture));
// 显示1,234.57
干杯,
Edo
引用:但是答案必须显示6.00
这与数字数据类型,即数字表示。
您可以使用Double.ToString(F2)
方法,请参阅MSDN上的标准数字格式字符串 [ ^ ]。
你可以按照这种方式......
qty = 2 跨度>;
rate = 3 ;
double Ans;
textBox3.Text = string .Format( {0:0.00},Convert.ToDouble(textBox1.Text)* Convert.ToDouble(textBox2.Text));
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
There's nothing to it, you just need to cast the double to a decimal, here's how:
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:
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
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 theDouble.ToString("F2")
method, see "Standard Numeric Format Strings" at MSDN[^].
You Can Follow this way....
qty=2; rate=3; double Ans; textBox3.Text =string.Format("{0:0.00}", Convert.ToDouble(textBox1.Text) * Convert.ToDouble(textBox2.Text));
这篇关于乘以双变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!