大家好,当我对任何数字进行数学运算* 1%时,它给了我一个很长的数字,我想将其减少到2个十进制的C# WPF转换



我在这里的问题是如何将数学结果转换为两个小数的结果?{ private void BtnAjoutInteret_Click(对象发送器,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.##}".

在此处查看更多内容

double x=1.123456;
var y= x.ToString("#.##"); 
var z= x.ToString("0.##"); 
Console.WriteLine("Value of Y " + y);
Console.WriteLine("Value of z " +z);

输出: Y 的值 1.12 z 的值 1.12

最新更新