question

AA-5973 avatar image
0 Votes"
AA-5973 asked ·

c# Replace dot with comma while input in WPF DataGrid

Hey,

I am doing a simple calculator as an exercise and I am quite new in c#.

How to change all dot to comma? E.g. 21.22 but I need it always to be like 21,22
Code XALM:

 <Window x:Class="Calculator.MainWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:local="clr-namespace:Calculator"
         mc:Ignorable="d"
         Title="MainWindow" Height="500" Width="300">
     <Grid Margin="8">
         <Grid.ColumnDefinitions>
             <ColumnDefinition/>
             <ColumnDefinition/>
             <ColumnDefinition/>
             <ColumnDefinition/>
         </Grid.ColumnDefinitions>
         <Grid.RowDefinitions>
             <RowDefinition Height="1.5*"/>
             <RowDefinition/>
             <RowDefinition/>
             <RowDefinition/>
             <RowDefinition/>
             <RowDefinition/>
         </Grid.RowDefinitions>
         <Label x:Name="result" Content="0" Grid.ColumnSpan="4" HorizontalAlignment="Right" 
                VerticalAlignment="Bottom" FontSize="50"/>
         <Button Content="AC" Margin="2" Grid.Column="0" Grid.Row="1"
                 FontWeight="bold" Click="OperationButton_Click"/>
         <Button Content="+/-" Margin="2" Grid.Column="1" Grid.Row="1"
                 FontWeight="bold" Click="OperationButton_Click"/>
         <Button Content="%" Margin="2" Grid.Column="2" Grid.Row="1"
                 FontWeight="bold" Click="OperationButton_Click"/>
         <Button Content="÷" Margin="2" Grid.Column="3" Grid.Row="1"
                 FontWeight="bold" Click="OperationButton_Click"/>
    
         <Button Content="7" Margin="2" Grid.Column="0" Grid.Row="2"
                 FontSize="20" Click="NumberButton_Click"/>
         <Button Content="8" Margin="2" Grid.Column="1" Grid.Row="2"
                 FontSize="20" Click="NumberButton_Click"/>
         <Button Content="9" Margin="2" Grid.Column="2" Grid.Row="2"
                 FontSize="20" Click="NumberButton_Click"/>
         <Button Content="×" Margin="2" Grid.Column="3" Grid.Row="2"
                 FontWeight="bold" Click="OperationButton_Click"/>
    
         <Button Content="4" Margin="2" Grid.Column="0" Grid.Row="3"
                 FontSize="20" Click="NumberButton_Click"/>
         <Button Content="5" Margin="2" Grid.Column="1" Grid.Row="3"
                 FontSize="20" Click="NumberButton_Click"/>
         <Button Content="6" Margin="2" Grid.Column="2" Grid.Row="3"
                 FontSize="20" Click="NumberButton_Click"/>
         <Button Content="-" Margin="2" Grid.Column="3" Grid.Row="3"
                 FontWeight="bold" Click="OperationButton_Click"/>
    
         <Button Content="1" Margin="2" Grid.Column="0" Grid.Row="4"
                 FontSize="20" Click="NumberButton_Click"/>
         <Button Content="2" Margin="2" Grid.Column="1" Grid.Row="4"
                 FontSize="20" Click="NumberButton_Click"/>
         <Button Content="3" Margin="2" Grid.Column="2" Grid.Row="4"
                 FontSize="20" Click="NumberButton_Click"/>
         <Button Content="+" Margin="2" Grid.Column="3" Grid.Row="4"
                 FontWeight="bold" Click="OperationButton_Click"/>
    
         <Button Content="0" Margin="2" Grid.Column="0" Grid.Row="5" 
                 FontSize="20" Grid.ColumnSpan="2" Click="NumberButton_Click"/>
         <Button x:Name="dec" Content="" Margin="2" Grid.Column="2" Grid.Row="5"
                 FontWeight="bold" Click="OperationButton_Click"/>
         <Button Content="=" Margin="2" Grid.Column="3" Grid.Row="5"
                 FontWeight="bold" Click="OperationButton_Click"/>
     </Grid>
 </Window>

Code C#:

 using System;
 using System.Globalization;
 using System.Windows;
 using System.Windows.Controls;
    
 namespace Calculator
 {
     /// <summary>
     /// Interaction logic for MainWindow.xaml
     /// </summary>
     public partial class MainWindow : Window
     {
         double firstNumber, secondNumber, resultNumber = 0;
         bool calcDone = false;
         Operations operation = Operations.None;
         string separator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
    
         public MainWindow()
         {
             InitializeComponent();
    
             //Assign to the decimal button the separator from the current culture
             dec.Content = separator;
         }
    
         //List the possible numeric operations
         private enum Operations
         {
             None,
             Division,
             Multiplication,
             Subtraction,
             Sum
         }
    
         //Manage number buttons input
         private void NumberButton_Click(object sender, RoutedEventArgs e)
         {
             Button button = (Button)sender;
    
             if (calcDone) //calculation already done
             {
                 result.Content = $"{button.Content}";
                 calcDone = false;
             }
             else //calculation not yet done
             {
                 if (result.Content.ToString() == "0")
                 {
                     result.Content = $"{button.Content}";
                 }
                 else
                 {
                     result.Content = $"{result.Content}{button.Content}";
                 }
             }
    
         }
    
         //Manage operation buttons input
         private void OperationButton_Click(object sender, RoutedEventArgs e)
         {
             Button button = (Button)sender;
    
             //Evaluate button pressed
             switch (button.Content.ToString())
             {
                 case "AC":
                     result.Content = "0";
                     break;
                 case "+/-":
                     if (!(result.Content.ToString() == "0"))
                     {
                         result.Content = Convert.ToDouble(result.Content) * -1;
                     }
                     break;
                 case "%":
                     if (!(result.Content.ToString() == "0"))
                     {
                         result.Content = Convert.ToDouble(result.Content) / 100;
                     }
                     break;
                 case "÷":
                     firstNumber = Convert.ToDouble(result.Content);
                     operation = Operations.Division;
                     result.Content = "0";
                     break;
                 case "×":
                     firstNumber = Convert.ToDouble(result.Content);
                     operation = Operations.Multiplication;
                     result.Content = "0";
                     break;
                 case "-":
                     firstNumber = Convert.ToDouble(result.Content);
                     operation = Operations.Subtraction;
                     result.Content = "0";
                     break;
                 case "+":
                     firstNumber = Convert.ToDouble(result.Content);
                     operation = Operations.Sum;
                     result.Content = "0";
                     break;
                 case "=":
                     switch (operation)
                     {
                         case Operations.Division:
                             if (calcDone)
                             {
                                 if (!(result.Content.ToString() == "ERROR"))
                                 {
                                     result.Content = Convert.ToDouble(result.Content) / secondNumber;
                                 }
                             }
                             else
                             {
                                 //Check if division by 0
                                 if (result.Content.ToString() == "0")
                                 {
                                     result.Content = "ERROR";
                                     calcDone = true;
                                 }
                                 else
                                 {
                                     secondNumber = Convert.ToDouble(result.Content);
                                     resultNumber = firstNumber / secondNumber;
                                     result.Content = resultNumber;
                                     calcDone = true;
                                 }
                             }
                             break;
                         case Operations.Multiplication:
                             if (calcDone)
                             {
                                 result.Content = Convert.ToDouble(result.Content) * secondNumber;
                             }
                             else
                             {
                                 secondNumber = Convert.ToDouble(result.Content);
                                 resultNumber = firstNumber * secondNumber;
                                 result.Content = resultNumber;
                                 calcDone = true;
                             }
                             break;
                         case Operations.Subtraction:
                             if (calcDone)
                             {
                                 result.Content = Convert.ToDouble(result.Content) - secondNumber;
                             }
                             else
                             {
                                 secondNumber = Convert.ToDouble(result.Content);
                                 resultNumber = firstNumber - secondNumber;
                                 result.Content = resultNumber;
                                 calcDone = true;
                             }
                             break;
                         case Operations.Sum:
                             if (calcDone)
                             {
                                 result.Content = Convert.ToDouble(result.Content) + secondNumber;
                             }
                             else
                             {
                                 secondNumber = Convert.ToDouble(result.Content);
                                 MessageBox.Show($"{firstNumber} + {secondNumber}");
                                 resultNumber = firstNumber + secondNumber;
                                 result.Content = resultNumber;
                                 calcDone = true;
                             }
                             break;
                     }
                     break;
                 default:
                     if (!result.Content.ToString().Contains(separator))
                     {
                         result.Content = $"{result.Content}{button.Content}";
                     }
                     break;
             }



dotnet-csharpwindows-wpf
· 2
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@AA-5973
I tested Viorel-1 answer, it worked in the calculated result value. If you want to display when typing, you could use CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator.

0 Votes 0 ·
AA-5973 avatar image AA-5973 DaisyTian-MSFT ·

Hey

Thank you for your answer!

My CulturalInfo is Sweden
So this calculator has coma when I do e.g. 3,5+6 but the result would be with a dot such this 9.5. (but I need it with coma also such 9,5)

Could you help me with this issue? )

I think in line 16 string separator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator; is a problem. When I I put the cursor on NumberDecimalSeparator then shows this info: "The default for NumberFormatInfo.InvarianInfo is ".". "

Moreover where should I write " <Label x:Name="result" Content="0" Language="es-ES" … /> " ?

THANKS!


Best regards,
Anastasiia




0 Votes 0 ·
DaisyTian-MSFT avatar image
0 Votes"
DaisyTian-MSFT answered ·

I updated the result as below:

 <Label x:Name="result" Content="0" Grid.ColumnSpan="4" HorizontalAlignment="Right" Language="es-ES" VerticalAlignment="Bottom" FontSize="50"/>

And Updated separator and OperationButton_Click like:

 string separator = CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator;

 .......
  private void OperationButton_Click(object sender, RoutedEventArgs e)
         {
             Button button = (Button)sender;
    
             //Evaluate button pressed
             switch (button.Content.ToString())
             {
                 case "AC":
                     result.Content = "0";
                     break;
                 case "+/-":
                     if (!(result.Content.ToString() == "0"))
                     {
                         result.Content = Convert.ToDouble((result.Content).ToString().Replace(separator, ".")) * -1;
                     }
                     break;
                 case "%":
                     if (!(result.Content.ToString() == "0"))
                     {
                         result.Content = Convert.ToDouble((result.Content).ToString().Replace(separator, ".")) / 100;
                     }
                     break;
                 case "÷":
                     firstNumber = Convert.ToDouble((result.Content).ToString().Replace(separator, "."));
                     operation = Operations.Division;
                     result.Content = "0";
                     break;
                 case "×":
                     firstNumber = Convert.ToDouble((result.Content).ToString().Replace(separator, "."));
                     operation = Operations.Multiplication;
                     result.Content = "0";
                     break;
                 case "-":
                     firstNumber = Convert.ToDouble((result.Content).ToString().Replace(separator, "."));
                     operation = Operations.Subtraction;
                     result.Content = "0";
                     break;
                 case "+":
                     firstNumber = Convert.ToDouble((result.Content).ToString().Replace(separator, "."));
                     operation = Operations.Sum;
                     result.Content = "0";
                     break;
                 case "=":
                     switch (operation)
                     {
                         case Operations.Division:
                             if (calcDone)
                             {
                                 if (!(result.Content.ToString() == "ERROR"))
                                 {
                                     result.Content = Convert.ToDouble((result.Content).ToString().Replace(separator,".") )/ secondNumber;
                                 }
                             }
                             else
                             {
                                 //Check if division by 0
                                 if (result.Content.ToString() == "0")
                                 {
                                     result.Content = "ERROR";
                                     calcDone = true;
                                 }
                                 else
                                 {
                                     secondNumber = Convert.ToDouble((result.Content).ToString().Replace(separator, "."));
                                     resultNumber = firstNumber / secondNumber;
                                     result.Content = resultNumber;
                                     calcDone = true;
                                 }
                             }
                             break;
                         case Operations.Multiplication:
                             if (calcDone)
                             {
                                 result.Content = Convert.ToDouble((result.Content).ToString().Replace(separator, ".")) * secondNumber;
                             }
                             else
                             {
                                 secondNumber = Convert.ToDouble((result.Content).ToString().Replace(separator, "."));
                                 resultNumber = firstNumber * secondNumber;
                                 result.Content = resultNumber;
                                 calcDone = true;
                             }
                             break;
                         case Operations.Subtraction:
                             if (calcDone)
                             {
                                 result.Content = Convert.ToDouble((result.Content).ToString().Replace(separator, ".")) - secondNumber;
                             }
                             else
                             {
                                 secondNumber = Convert.ToDouble((result.Content).ToString().Replace(separator, "."));
                                 resultNumber = firstNumber - secondNumber;
                                 result.Content = resultNumber;
                                 calcDone = true;
                             }
                             break;
                         case Operations.Sum:
                             if (calcDone)
                             {
                                 result.Content = Convert.ToDouble((result.Content).ToString().Replace(separator, ".")) + secondNumber;
                             }
                             else
                             {
                                 secondNumber = Convert.ToDouble((result.Content).ToString().Replace(separator, "."));
                                 //MessageBox.Show($"{firstNumber} + {secondNumber}");
                                 resultNumber = firstNumber + secondNumber;
                                 result.Content = resultNumber;
                                 calcDone = true;
                             }
                             break;
                     }
                     break;
                 default:
                     if (!result.Content.ToString().Contains(separator))
                     {
                         result.Content = $"{result.Content}{button.Content}";
                     }
                     break;
             }

Result picture is:
80009-2.gif


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2.gif (56.0 KiB)
· 2 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Wow! Cool. Thank you!
I have one more issue regarding this code. Is it possible to ask you about it?

0 Votes 0 ·

@AA-5973
I have already replied to your other question, please check and point out if my answer doesn't meet your need.

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered ·

Hello,

You can format via using a different culture and format. In this code sample I hard-coded the value.

 result.Content = string.Format(System.Globalization.CultureInfo.GetCultureInfo("de-DE"), "{0:0.0}", 114.3);



·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered ·

If you want to change the format of the label using some specific language, such as Spanish, for example, then try adding the Language attribute:

<Label x:Name="result" Content="0" Language="es-ES" … />

This will use comma as decimal separator.

It is also possible to set the language for the whole <Window> or application.

· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hey

Thank you for your answer!
My CulturalInfo is Sweden
So this calculator has coma when I do e.g. 3,5+6 but the result would be with a dot such this 9.5
(but I need it with coma also such 9,5)

Could you help me with this issue? )

I think in line 16 string separator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator; is a problem. When I I put the cursor on NumberDecimalSeparator then shows this info: "The default for NumberFormatInfo.InvarianInfo is ".". "

Best regards,
Anastasiia



0 Votes 0 ·