0

So for example if you run: (Math.Round(100 * (4666 / (double)82343), 2)) on rextester.com it returns 5,67 instead of 5.67 how can I make sure this operation and this operation ONLY returns a dot (while leaving it as , for everything else)

I don't want to .replace it in case other characters are possibly used like (space) which i've seen used before.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Ma Dude
  • 477
  • 1
  • 5
  • 17
  • 2
    This is a matter of globalization. In the Culture rextester uses, the decimal point is represented with a comma. – Zohar Peled Oct 04 '18 at 04:29
  • Possible duplicate of [convert float to string with . instead of ,](https://stackoverflow.com/questions/43374167/convert-float-to-string-with-instead-of) – Lance U. Matthews Oct 04 '18 at 04:40
  • Math.Round() never formats a floating pointer number. Don't get confused by what the debugger or a random website shows you. – Hans Passant Oct 04 '18 at 06:24

1 Answers1

3

Use System.Globalization.CultureInfo.InvariantCulture as format provider

var result = (Math.Round(100 * (4666 / (double)82343), 2));
var output = result.ToString(System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine(output);

By default .Net application uses local machine culture information.
By providing custom culture you can change this behaviour.

CultureInfo.InvariantCulture is a predefined culture which has a dot . as NumberDecimalSeparator

Fabio
  • 31,528
  • 4
  • 33
  • 72
  • Thank you knew it was CultureInfo related but didn't know exactly how to handle this operation. Thank you – Ma Dude Oct 04 '18 at 04:35