大家好,当我计算任何数字*1%时,它给了我一个很长的数字,我想把它减少到2个十进制C#WPF转换
我的问题是如何将数学结果转换成两位小数的结果{ private void BtnAjoutInteret_单击(对象发送者,路由目标)//当我按下确定按钮时大家好,当我计算任何数字*1%时,它给了我一个很长的数字,我想把它减少到2个十进制C#WPF转换,c#,wpf,C#,Wpf,我的问题是如何将数学结果转换成两位小数的结果{ private void BtnAjoutInteret_单击(对象发送者,路由目标)//当我按下确定按钮时 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
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();
} }
试试看
{
对于(int i=1;i
双d=1.2345;
string s=string.Format(“{0:0.##}”,d);
或直接:
double d=1.2345;
StreamWriter Writer=新的StreamWriter(文件名);
Writer.WriteLine({0:0.}),d);
您还可以使用类似于“{0,6:0.##}”
的方法添加自动间距
看更多
输出:
Y值1.12
z1.12的值是否回答了您的问题?
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);