Is there any Math function for this in c#?
If value is 18.00 then i want value 18, If value is 18.50 then i want value 18.5, If value is 18.25000 then i want value 18.25.
Is there any Math function for this in c#?
If value is 18.00 then i want value 18, If value is 18.50 then i want value 18.5, If value is 18.25000 then i want value 18.25.
If you want to convert to decimal only use this
static decimal? RemoveTrailingZeros(this decimal? value)
{
if (value == null) return null;
var format = $"0.{string.Join(string.Empty, Enumerable.Repeat("#", 29))}";
var strvalue = value.Value.ToString(format);
return ConvertToDecimalCultureInvariant(strvalue);
}
static decimal? ConvertToDecimalCultureInvariant(this string value)
{
decimal decValue;
if (!decimal.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out decValue))
{
return null;
}
return decValue;
}
Since the precision of a decimal is 29 hence Enumerable.Repeat("#", 29)
.
And use it as
var result = RemoveTrailingZeros(29.0000m);