5

I've this problem and can't find a solution. This is super easy and i don't know why can't i find a solution.

Problem:

  • if a value returns for example "16.60", in c# i'll read "16.6", but i need 0 as well, because of paypal API, wich only accepts a value with no decimal numbers, or if it has to have decimal numbers the minimum and maximum must be 2.

So how can i make this?

i've tried this:

        string value_f = "16,6";
        decimal value_f_d = decimal.Parse(value_f);
        value_f_d = (decimal)Math.Round(value_f_d, 2);
        value_f = value_f_d.ToString("#.##");
        value_f = value_f.Replace(',', '.');

i want this output: 16.60, but gives this: 16.6

Severiano
  • 1,083
  • 3
  • 25
  • 54
  • 5
    Did you read the documentation of the `ToString` overload that takes a format string? What does it tell you `#` means? –  Jul 13 '14 at 20:13

1 Answers1

31
string output = value_f_d.ToString("#.00", CultureInfo.InvariantCulture);

(using System.Globalization in your using declarations at the top)

PulseLab
  • 1,577
  • 11
  • 15
  • 3
    Additionally, consider using the InvariantCulture to format your number. That avoids the necessity of replacing the decimal separator. I.e. `var output = value_f_d.ToString("#.00", System.Globalization.CultureInfo.InvariantCulture)` – dbc Jul 13 '14 at 20:18
  • @dbc, good call - answer updated. Thanks. – PulseLab Jul 13 '14 at 20:20