我试图建立一个简单的小费计算器并获得小费应该是什么的精确值。我已经完成了大部分工作,但是我正在努力寻找小数点后的确切两个数字,因为它会使它们四舍五入。有人可以帮忙吗?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace project01LEA
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Close();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Lyndsee Armstrong\nCSIS 1400\nProject #1");
}
private void textBox1_Leave(object sender, EventArgs e)
{
//define variables
const double POOR = 0.10;
const double AVERAGE = 0.15;
const double EXCELLENT = 0.20;
double mealAmount = Convert.ToDouble(textBox1.Text);
double doubledPoor = Convert.ToInt32(POOR * mealAmount);
double doubledAverage = Convert.ToInt32(AVERAGE * mealAmount);
double doubledExcellent = Convert.ToInt32(EXCELLENT * mealAmount);
string outStr = string.Format("{0:C2}", doubledPoor);
string outStr1 = string.Format("{0:C2}", doubledAverage);
string outStr2 = string.Format("{0:C2}", doubledExcellent);
textBox2.Text = outStr;
textBox3.Text = outStr1;
textBox4.Text = outStr2;
}
}
}
答案 0 :(得分:2)
您的问题是Convert.ToInt32
- 将浮点值更改为整数值,而整数不会保留小数值。如果你删除这些行,这应该完全按照你想要的方式工作(字符串格式看起来正确)。