c# - 大家好,当我计算任何数字 * 1% 时,它给了我一个很长的数字,我想将其缩减为 2 位小数 C# WPF 转换
问题描述
我的问题是如何将数学结果转换为两位小数的结果?{ private void BtnAjoutInteret_Click(object sender, RoutedEventArgs e)//当我按下确定按钮时
try
{
for (int i = 1; i < clients.ListesClients.Count; i++)// For all the bank clients add //one percent to the sum why is it so hard to post a question on stackoverflow
{
double interet = .01;
clients.ListesClients[i].Balance = (clients.ListesClients[i].Balance * interet) + (clients.ListesClients[i].Balance);
clients.AjustementCompte(clients.ListesClients[0]);//once the one percent is added use the methode to add the new balance to the txtfile.
}
MessageBox.Show("Transaction accepter");
LviewListeClients.Items.Refresh();
}
catch (Exception)
{
MessageBox.Show("erreur 5 ");
return;
}
}
public void AjustementCompte(Client Nouvelle)//This is the method to add the new balance
{
//listeClients.Add(NouvelleTransaction);
StreamWriter Writer = new StreamWriter(filename);
foreach (Client client in ListesClients)
{
Writer.WriteLine($"{client.ID};{client.TypeDeCompte};{client.Balance}");
}
Writer.Close();
} }
解决方案
double d = 1.2345;
string s = string.Format("{0:0.##}", d);
或直接:
double d = 1.2345;
StreamWriter Writer = new StreamWriter(filename);
Writer.WriteLine("{0:0.##}", d);
您还可以使用类似"{0,6:0.##}"
.
在这里查看更多