How to ignore value after first decimal point first value if it is 0 else take it if greater than 0?

后端 未结 2 1062
别那么骄傲
别那么骄傲 2021-01-14 05:59

I am having a value like below :

decimal val = 1.100;

Now what i am trying to do is if i am having 0 after first decimal point value then i

相关标签:
2条回答
  • 2021-01-14 06:16

    You can use a custom numeric format string:

    val.ToString("0.0##")
    
    0 讨论(0)
  • 2021-01-14 06:20

    Hope you like this logic. Please try this.

      double val = 1.002;
      string output = !val.ToString().TrimEnd('0').Contains('.') ? string.Format("{0}.0", val) : val.ToString().TrimEnd('0');
    

    If the value is 1.00, then my output will be 1.0

    Then you can convert this string into other data types.

    0 讨论(0)
提交回复
热议问题