Question How do I get output values with commas instead of dot?

Acke

New member
Joined
Jan 23, 2021
Messages
1
Programming Experience
Beginner
C#:
{
    float value1, value2, value3 = 0F;

    Console.Write("\n\nEnter the first number!\n");

    value1 = float.Parse(Console.ReadLine());

    Console.Write("Enter the second number!\n");

    value2 = float.Parse(Console.ReadLine());

    Console.Write("Enter the third number!\n");

    value3 = float.Parse(Console.ReadLine());

    Console.WriteLine("{0}\t{1}\t{2}", value1, value2, value3);
}
 
Last edited by a moderator:
It the future post your code in code tags. I've modified your post above.

The use of commas vs. dots/periods to denote decimal points is dependent on the culture that your code is running in. You can tell the .NET Framework to use a different UI culture instead of using the default culture that is based on the OS culture settings.

C#:
using System;
using System.Globalization;

class Program
{
    static void Main()
    {
        double value = 1.23;

        Console.WriteLine(CultureInfo.CurrentCulture.Name);
        Console.WriteLine(value);

        CultureInfo.CurrentCulture = new CultureInfo("es-ES");
        Console.WriteLine(CultureInfo.CurrentCulture.Name);
        Console.WriteLine(value);
    }
}

Produces the following output on my machine:
C#:
en-US
1.23
es-ES
1,23
 
Last edited:
Parse is brazen and reckless unless you have certainty of your users inputs and that they are sanitised and precisely typed as expected. TryParse is careful, and returns bool true/false if a parse is successful or not.

I use the word sanitise because it's suitable for describing the opposite to dirty, shit, sewage and it is the complete opposite to dirty. Sanitise your inputs first.
value1 = float.Parse(Console.ReadLine());
Where are you verifying the user input is a given format before parsing it?
Only use parse if you know the format you are trying to parse.

Moved to console applications.
 
@Sheepings, while I don't disagree with what you said, one thing to keep in mind is that, often, assignments set to beginners specifically assume that inputs will be valid and are marked on that basis. That's because the point is to teach something specific and things like validation can obfuscate the actual purpose. Maybe that's the case here and maybe it's not.
 
@jmcilhinney, While I don't disagree with you either... Would it be your esteemed opinion that a good teacher would and should teach why sanitised inputs should be validated before teaching them how to parse such inputs? Rather than parsing and possibly causing a validation/parse exception for providing a mistaken or wrong input, and only to later ridicule them for the fact that the user inputs should nearly always be sanitised first. If your above post were true; then this is deliberately misleading the students past a chapter they should have learned beforehand. And If I were new to this, I might find that misleading and confusing.

To me it seems more logical to teach students how to think, and not what to think. Ie. Here's an assignment, send it these inputs, and tell me if it outputs these values etc. I was the kind of kid in school, who would do the opposite to what I was told, all so I could find out why they didn't want me to do something alternatively. All my life I've looked for alternative ways of doing things. But that's just me.
 
Back
Top Bottom