0

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.

Hitesh Patil
  • 380
  • 4
  • 15

2 Answers2

1

You can use Decimal.Round the documentation is here

Deciaml.Round Documentation

Vahid Vakily
  • 306
  • 1
  • 7
-1

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

enter image description here

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208