How to print a decimal value with comma ex: 75.0 would be 7.5?

Asked

Viewed 88 times

1

using System; 

class teste {

    static void Main(string[] args) 
    { 
        float nota1, nota2, MEDIA;
        med1;
        nota1=float.Parse(Console.ReadLine());
        nota2=float.Parse(Console.ReadLine());

        MEDIA=((nota2+nota1)/2);

        string med1 = MEDIA.ToString("0.00"); //EU GOSTARIA DE IMPRIMIR O VALOR COMO EX: 7.5 E NÃO 75,0
        Console.WriteLine(media1);  
    }
    
}
  • 1

    "I WOULD LIKE TO PRINT THE VALUE AS EX: 7.5" if you want the dot format do only .ToString()

  • This answers your question? Modify note value output

  • What are the values passed to Nota1 and nota2? Pq being passed the correct values you need do nothing.

  • "75.0 would be 7.5" - This is confusing, because 75,0 is seventy-five, and 7,5 is seven and a half, and the question is still said "7.5 AND NOT 75.0" (that is, in addition to changing the value, also changed the point instead of the comma). Your problem is that the value is going wrong, it is only formatting (number of decimals, decimal separator, etc), or both?

2 answers

1


You can format the value of your variable MEDIA with string. Format:

string med1 = string.Format("{0:0.0}", MEDIA);

The result would be (based on the comment of the code that is in the question):

7.5

0

Convert the numeric value to its equivalent string representation using a specified format with the method Single.ToString(string format, IFormatProvider provider) where:


using System;
using System.Globalization;

class Program {
    static void Main(string[] args) 
    { 
        float nota1, nota2, MEDIA;
        nota1=float.Parse(Console.ReadLine());
        nota2=float.Parse(Console.ReadLine());

        MEDIA=(nota2+nota1)/2;

        Console.WriteLine(MEDIA.ToString("F", new NumberFormatInfo{
                NumberDecimalSeparator = ".",
                NumberDecimalDigits = 1
        }));  
    }
}

Test the code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.