Decimal point gets removed while converting C# C to decimal

In order to support the group separator, a specific notation can be used. It should be noted that the notation used stands for the decimal separator and group separator on your workstation. If you want to treat the notation as a decimal separator (so that the group separator will be parsed into it), an alternative notation can be specified. There are varying decimal separators across different countries. This solution was found with the help of @ReedCopsey on the post “How to remove decimal point from a decimal number in c#?”.


Solution 1:

During string conversion, the current culture is utilized, and it may not be uniform in various aspects such as decimal or thousands separator. However, you have the option to utilize the invariant culture for any of these conversions.

decimal d = Convert.ToDecimal(Console.ReadLine(), CultureInfo.InvariantCulture);
decimal d = Decimal.Parse(Console.ReadLine(), CultureInfo.InvariantCulture);

An alternative is to establish the default culture when initializing your application, which will have an impact on all subsequent conversions.

CultureInfo.CurrentCulture = CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;


Solution 2:


In case you utilize

decimal

for financial purposes, it’s recommended to allow for the inclusion of group (thousand) separators along with decimal separators.

   123,456,789.45

Within

.

, the decimal separator is utilized, while

,

serves as group ones. If you require a group separator, you can indicate it using

NumberStyles.Any

.

   Console.Write("Enter number: ");
   decimal d;
   while (!Decimal.TryParse(Console.ReadLine(), NumberStyles.Any, null, out d)) 
     Console.WriteLine("Invalid syntax. Please, try again."); 

Kindly take note that in this section, I have utilized the code

null

, which represents

CultureInfo.CurrentCulture

. It appears that at your station,

,

is utilized as the decimal separator, while

.

is used for the grouping.

   "123.45"    -> 12345m   - note, that group separator(s) is/are removed
   "1.2.3.4.5" -> 12345m
   "123,45"    -> 123.45m  - while decimal is turned into decimal point  

To consider

.

as a decimal separator and parse

"123.45"

into

123.45m

, you have the option to specify

CultureInfo.InvariantCulture

.

   Console.Write("Enter number: ");
   decimal d;
   while (!Decimal.TryParse(Console.ReadLine(), 
                            NumberStyles.Any, 
                            CultureInfo.InvariantCulture, 
                            out d)) 
     Console.WriteLine("Invalid syntax. Please, try again."); 


Solution 3:


As previously noted in the comments, the decimal separator differs from country to country. I recommend manually substituting the entered symbol with the separator that is customary in your culture.

string separator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
decimal d = Decimal.Parse(Console.ReadLine().Replace(".", separator).Replace(",", separator));

Frequently Asked Questions

Posted in Uncategorized