-3

My question here is how can i convert the result of the math into a two decimal result?{ private void BtnAjoutInteret_Click(object sender, RoutedEventArgs e)//When i press the ok button

        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();
    } }
  • 2
    Does this answer your question? [Have decimal amount, want to trim to 2 decimal places if present](https://stackoverflow.com/questions/4695053/have-decimal-amount-want-to-trim-to-2-decimal-places-if-present) – devNull Dec 28 '19 at 05:08

2 Answers2

0
double d = 1.2345;
string s = string.Format("{0:0.##}", d);

or directly:

double d = 1.2345;
StreamWriter Writer = new StreamWriter(filename);
Writer.WriteLine("{0:0.##}", d);

You can also add automatic spacing with something like "{0,6:0.##}".

See more here

beothunder
  • 551
  • 2
  • 14
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);

Output: Value of Y 1.12 Value of z 1.12