I have tried to do this conversion(decimal? to string) using information from few similar posts, but somehow failed("G29"; replace("0,00","")...) so what would be the best way to transform 91,5M decimal? to string so that we would have 91,5?
string a;
decimal? number = 91.5M;
a = number.ToString();
in this example a = 91.5 and everything is fine. But for some strange reason when I get a bunch of objects from Oracle db and try to convert them to this specific format without trailing zeros, I just can't find a way to do this, because always the final output is 91,50 instead of 91,5
var a = number.ToString("G29");
This should remove your trailing zeroes in the output string. There is a potential flaw in this in as much as very small values will be represented "xEx" format. Not sure if this is a problem for you.
If you specifically want up to one decimal position with no leading zeros, you can try this:
string str = number.ToString("0.#");
Adding more # will increase precision, but result will not contain non-significant digits.
Replace # with 0 in case you have to keep trailing zeroes.
You also can specify the format for the separator, with a second IFormatProvider parameter:
string str = number.ToString("0.#", fr-FR); // 5 => 5 5,123 => 5,1 (comma)
string str = number.ToString("0.#", en-US); // 5 => 5 5,123 => 5.1 (dot)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With