Math.Round vs String.Format

前端 未结 6 1771
天命终不由人
天命终不由人 2021-01-18 18:24

I need double value to be rounded to 2 digits. What is preferrable?

String.Format(\"{0:0.00}\", 123.4567);      // \"123.46\"
Math.Round(123.4567, 2)                 


        
6条回答
  •  我在风中等你
    2021-01-18 18:46

    They are different functions, if you need the output to be displayed, use the first one (that also forces decimals to appear). You will avoid the overhead of the inevitable .ToString() that will occur if the variable is of type double.

    Note that the second one rounds the number but if it's an integer result, you will get just the integer (ie: 7 vs 7.00)

提交回复
热议问题