3

I am getting a value that looks like 17.55453304545 (just as an example), from a database. But I want it to show with one decimal, so that it looks like: 17.6, before it is shown to the aspx page. I don't have access to change the value in tha database tabel, so I must do it in the C# code instead. I have tried:

double result = (Convert.ToDouble(this.GetFieldValue("value"));

But the value does not change. Come to think of it, then I think that it is because I use double? The value is probably a double at first.

user1960836
  • 1,732
  • 7
  • 27
  • 47
  • 1
    How do you show the value? You have to apply formatter there (something like `double.ToString("R1"`) – Sinatr Dec 20 '13 at 09:49

5 Answers5

10

You should use string formatting to display it, rather than displaying the raw value. For example,

String.Format("{0:0.0}", myDoubleValue)
Richard
  • 29,854
  • 11
  • 77
  • 120
6

Use Math.Round:

double result = Math.Round(this.GetFieldValue("value"), 1);

You can change 1 to any other number of decimal places. refer to this post for more info How do you round a number to two decimal places in C#?

Community
  • 1
  • 1
Nati Cohen
  • 230
  • 4
  • 11
2

If you only want to show it use:

var value = (17.55453304545).ToString("#.#");
Odrai
  • 2,163
  • 2
  • 31
  • 62
1

This should do the trick

var formatted = result.ToString("f1");
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
1

Try this:

double roundUp = Math.Round(Value, 1, MidpointRounding.AwayFromZero); 
Vishal
  • 604
  • 1
  • 12
  • 25