0

I'm trying to remove the trailing zeros from a decimal value taken from a database. In particular the value is stored as 800.000 but I need to display 800, so I need to remove the zeros, I tried:

string value = "800.000";
string converted = value.ToString("G29");

I get: 800000 but should be: 800, I checked this question and tried also different solution but I get the same result.

What I'm doing wrong?

Charanoglu
  • 1,229
  • 2
  • 11
  • 30
  • As a first clarification I should ask. Why do you have your numbers stored in a database as strings? – Steve Jun 29 '18 at 18:48

4 Answers4

1

You should consider your culture when converting numbers expressed as strings.

string converted = Convert.ToDecimal(value, CultureInfo.InvariantCulture).ToString("G29");
Steve
  • 213,761
  • 22
  • 232
  • 286
0

Simple Solution:-

 string data = "900.00";
            double result=0;
            double.TryParse(data,out result);
            int ab = (int)result;
Hitesh Anshani
  • 1,499
  • 9
  • 19
-1

You can use any of the below-mentioned ways -

string.Format("{0:G29}", decimal.Parse("2.0044"))

decimal.Parse("2.0044").ToString("G29")

2.0m.ToString("G29")
Salvatore
  • 1,435
  • 1
  • 10
  • 21
-1

If you are looking to get a whole number from a decimal, below can work

var decimalValue = 800.000M;
var wholeNumber = Math.Truncate(decimalValue);
Viju
  • 95
  • 1
  • 7