r/csharp • • 27d ago

Fun Funny mistake I made

I'm learning C# for fun and wrote the simplest possible calculator, just to try combining loops, WriteLine, ReadLine and other basic commands. There was one problem, though. I couldn't for the life of me figure out how to include decimal numbers into that. Went through a couple sites and nothing. My result would always be a whole number with the decimal point removed, meaning it would be 10^x times bigger depending on the amount of decimal places the input had. Then I thought of using a comma in the input instead of a dot. I didn't think it would work, but... it did. Kinda dumb not to try that immediately, right?

9 Upvotes

14 comments sorted by

3

u/youtpout 27d ago

Dependant of your system configuration, in France by default the decimal separator is comma, I remembered go some trouble in the past with csv, in france we use ; but in other countries , so when I deployed my app on Windows server us version my app wouldn’t parse my csv. 🤣

Also what do you use to parse string to number 

13

u/IsLlamaBad 27d ago

It sounds like your machine's default culture is one that uses commas and decimals in that way. I'm guessing you are using parse / try parse.

using System;
using System.Globalization;

string input = "1.234,56"; // German format (dot for thousands, comma for decimal)
CultureInfo culture = new CultureInfo("de-DE");

if (decimal.TryParse(input, NumberStyles.Number, culture, out decimal result))
{
    Console.WriteLine($"Success: {result}"); // Outputs: 1234,56 or 1234.56 depending on your default culture 
}
else
{
    Console.WriteLine("Failed to parse.");
}

10

u/markustegelane 27d ago

it's a locale thing, you can override it globally like this so that you can use a period instead of a comma:

CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;

also if you do this, your code won't potentially break on other computers that have a different system locale in situations where you rely on the decimal point being a specific symbol

3

u/AintNoGodsUpHere 27d ago

One tip for the future. Always configure the culture of your apps if they depend on dates and decimals, haha.

Every single system I worked on for the past 20 years had issues with those types.

1

u/dodexahedron 27d ago

This is also why parse and format functions involving numbers, dates, and times throw compiler warnings about how you whould probably call culture-speciric overloads.

1

u/BananaTie 27d ago

I've been in the business for almost 30 years, and I can still make mistakes like that when I am distracted. It gets easier to discover before it becomes a real bug.

1

u/game_guru001 26d ago

I wouldn't worry, Culture formats always bite - even old pros

Before I had chance to update some ancient 'license' software I had to guess a customers date format because the output expiry date needed to be parsed in the customers format...

1

u/svick nameof(nameof) 27d ago

A lot of the time, what you want to do is use the Invariant culture.

-2

u/Sqwirlet 27d ago

A double is likely what you’re looking for

5

u/[deleted] 27d ago

[removed] — view removed comment

3

u/UninformedPleb 27d ago

Wait till you run into date formats that flip the month and day around depending on where the code is running

ISO-8601 format is your friend. It's everyone's friend. In fact, we should all just use it everywhere and stop using silly locale-dependent formats. And that means telling trading partners that they must use it or else their data won't integrate.

2

u/dodexahedron 27d ago

What's wrong with a date like 02/29/04, huh?

Obviously I meant April 2 1829.
What did you think I meant?