C# 我怎样才能使它不会使货币四舍五入呢?
我一直在尝试构建一个简单的小费计算器,并获得小费应该是多少的精确值。我已经完成了大部分,但我正在努力找到小数点后的两个精确数字,因为它一直在舍入它们。有人能帮忙吗C# 我怎样才能使它不会使货币四舍五入呢?,c#,decimal,C#,Decimal,我一直在尝试构建一个简单的小费计算器,并获得小费应该是多少的精确值。我已经完成了大部分,但我正在努力找到小数点后的两个精确数字,因为它一直在舍入它们。有人能帮忙吗 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System
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;
}
}
}
您的问题是
Convert.ToInt32
——它将浮点值更改为整数值,而整数不包含小数。如果您删除了这些行,这应该完全符合您的需要(字符串格式看起来很正确)。在显示代码时,您应该标记语言(这有助于人们找到您的问题,也会影响语法突出显示)。我给你加上了c标签。