-3

How can I format this textbox to GBP currency, like 0.00?:`

private void textBox_LostFocus(object sender, RoutedEventArgs e)
{
    double amount = 0.0d;
    if (Double.TryParse(textBox.Text, NumberStyles.Currency, null, out amount))
    {
        textBox.Text = amount.ToString("C");
    }
}

That code is rupees but I was just testing it. I couldnt reall find anything much useful. The Textbox xaml:

 <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="51" Margin="10,461,0,0" TextWrapping="Wrap" Text="{Binding TotalValue}" VerticalAlignment="Top" Width="120" KeyDown="textBox_KeyDown" TextChanged="textBox_TextChanged_1" LostFocus="textBox_LostFocus"/>

The value right now is just something like 1.5 how can it be made to 1.50?

The TotalValue should be set to the formated currency.

private double totalValue;

public double TotalValue
{
    get { return totalValue; }
    set
    {
        totalValue = value;
        OnPropertyChanged("TotalValue");
    }
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    Menu.PassedData data = e.Parameter as Menu.PassedData;

    if (data != null) //If data is not 0 
    {
        PassedData.Add(data); //Increment data in list view
        double tempTotalValue = 0;
        foreach (var record in PassedData)
        {
            tempTotalValue = tempTotalValue + record.Value;
        }
        TotalValue = tempTotalValue;
    }
}
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • string.format can help you like string.format("{0:2D}") – Willie Cheng Apr 10 '16 at 23:02
  • other way ,please check [URL](http://stackoverflow.com/questions/3122677/add-zero-padding-to-a-string) ,by the way please pay more attention to your question that is duplicate – Willie Cheng Apr 10 '16 at 23:04
  • 1
    It's not clear what you're trying to ask. `string.Format("{0:C2}", value)` will give you two decimal places, pass a culture along with that for culture-speciic stuff like GBP (£). – stuartd Apr 10 '16 at 23:05

1 Answers1

0

Format using this technique for foreign monetary symbols...

 var totalValue = 123.45;

        string totalValueUKText = totalValue.ToString("C", new System.Globalization.CultureInfo("en-GB")); //display UK "£123.45";
        string totalValueUSText = totalValue.ToString("C", new System.Globalization.CultureInfo("en-US")); //display US "$123.45";

And your code would be something like this...

  private void textBox_LostFocus(object sender, RoutedEventArgs e)
   {
      double amount = 0.0d;
      if (Double.TryParse(textBox.Text, NumberStyles.Currency, null, out amount))
    {
        textBox.Text = amount.ToString("C", new System.Globalization.CultureInfo("en-GB"));
    }
}
DV8DUG
  • 247
  • 2
  • 9