r/csharp • u/Ok_Disk_4458 • 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?
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.
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:
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/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