Truncate number of digit of double value in C#

后端 未结 12 781
死守一世寂寞
死守一世寂寞 2020-12-30 04:35

How can i truncate the leading digit of double value in C#,I have tried Math.Round(doublevalue,2) but not giving the require result. and i didn\'t find any other method in M

12条回答
  •  滥情空心
    2020-12-30 04:54

    If you are looking to have two points after the decimal without rounding the number, the following should work

    string doubleString = doublevalue.ToString("0.0000"); //To ensure we have a sufficiently lengthed string to avoid index issues
    Console.Writeline(doubleString
                 .Substring(0, (doubleString.IndexOf(".") +1) +2)); 
    

    The second parameter of substring is the count, and IndexOf returns to zero-based index, so we have to add one to that before we add the 2 decimal values.

    This answer is assuming that the value should NOT be rounded

提交回复
热议问题