当我的Windows窗体上的数字相加时,0会被截止。 [英] When numbers are summed on my Windows Form, the 0's are cut off the answer.
问题描述
我创建了一个Windows Form长话短说。如果我在框中键入3.00,最后它将显示为3而不是3.00。为什么要摆脱所有的零?
我期待这是一个简单的解决方案,这里是代码,不要害怕。我确信解决方案很简单。
I have created a Windows Form long story short. If i type in 3.00 into the box, at the end it will show as 3 not 3.00. Why is it getting rid of all the zero's?
I'm expecting this to be a simple solution, here is the code, don't be scared. I'm sure the solution is simple.
public partial class Form1 : Form
{
double[] _prices = new double[10];
int _index;
string _outputDouble = "";
double _total = 0.00;
double _vat = 0.00;
private void Display()
{
_outputDouble = "";
foreach (double price in _prices)
{
_outputDouble = _outputDouble + price + "\r\n";
}
displayTextBox.Text = _outputDouble;
}
private void Total()
{
foreach (double price in _prices)
{
_total = _total + price;
}
totalDisplay.Text = _total.ToString();
}
private void VAT()
{
_vat = _total * 17.5 / 100 ;
vatDisplay.Text = _vat.ToString();
}
private void TotalVAT()
{
double totalVAT = 0.00;
totalVAT = _total + _vat;
totalVATDisplay.Text = totalVAT.ToString();
}
private void MinMax()
{
double max = 0.00;
double min = 0.00;
for (_index = 0; _index < 10; _index++)
{
if (_prices[_index] > max)
{
max = _prices[_index];
}
if (0 == _index)
min = _prices[_index];
else if (_prices[_index] < min)
min = _prices[_index];
}
maxPriceDisplay.Text = max.ToString();
minPriceDisplay.Text = min.ToString();
}
public Form1()
{
InitializeComponent();
}
private void enterPriceButton_Click(object sender, EventArgs e)
{
if (_index < 10)
{
_prices[_index] = double.Parse(priceTextBox.Text);
priceTextBox.Text = "";
_index++;
}
else
{
enterPriceButton.Enabled = false;
Display();
}
}
private void calcTotalButton_Click(object sender, EventArgs e)
{
Total();
VAT();
TotalVAT();
MinMax();
calcTotalButton.Enabled = false;
}
}
推荐答案
浮点数3与3.00相同;没有任何东西被削减。
当你应该显示一些数字时,你实际上使用通过double.ToString $ c $获得的字符串表示C>。要控制此表示,您只需要学习字符串格式。请参阅:
http://msdn.microsoft.com /en-us/library/system.double.tostring.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx [ ^ ]。
-SA
The floating number 3 is the same as 3.00; nothing is cut.
When you should show some number, you actually use string representation of it obtained viadouble.ToString
. To control this representation, all you need is to learn string formatting. Please see:
http://msdn.microsoft.com/en-us/library/system.double.tostring.aspx[^],
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^].
—SA
您可以使用ToString(。0 ##)
,并使用#想要的小数#.eg
You can useToString(".0##")
with as much # as decimals you want.e.g.
string strvalue = value.ToString(".0##");
对于浮动,您可以在.0之后使用6#,对于双倍,您可以使用15#。
通过这个你可以更好地理解转换和我还建议你在上面的答案中浏览sergey提供的链接。它以简洁的方式解释所有内容。
问候......:笑:
For float,you can use 6 #s after the .0 and for double you can use 15 #s.
By this you can have better understanding of conversion and i also recommend you to go through the links provided by sergey in above answer.It explains everything in a concise way.
Regards.. :laugh:
这篇关于当我的Windows窗体上的数字相加时,0会被截止。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!