How to limit decimals using C #

2

I'm having some trouble limiting the decimal places in C #.

double x = 1,41293

I wanted to output only from 1.412 I have already tested the following code:

Convert.ToDecimal(x).ToString("0.00", CultureInfo.InvariantCulture)

and

Convert.ToDecimal(x).ToString("N3", CultureInfo.InvariantCulture)
    
asked by anonymous 04.10.2017 / 13:06

1 answer

2

If it is a string, comma as a decimal separator, put CultureInfo into Convert :

decimal x = Convert.ToDecimal("1,41293", new CultureInfo("pt-BR"));

Console.WriteLine(x.ToString("N3"));    //Resultado: 1.413

If necessary, output also comma decimal separator, set CultureInfo to ToString() :

Console.WriteLine(x.ToString("N3", new CultureInfo("pt-BR") ));   //Resultado: 1,413
    
04.10.2017 / 13:28