简体   繁体   中英

Cannot remove trailing zeros from decimal

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?

在转换以字符串表示的数字时,应考虑您的文化。

string converted = Convert.ToDecimal(value, CultureInfo.InvariantCulture).ToString("G29");

Simple Solution:-

 string data = "900.00";
            double result=0;
            double.TryParse(data,out result);
            int ab = (int)result;

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")

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

var decimalValue = 800.000M;
var wholeNumber = Math.Truncate(decimalValue);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM