How do I display a decimal value to 2 decimal places?

前端 未结 17 2188
没有蜡笔的小新
没有蜡笔的小新 2020-11-21 23:24

When displaying the value of a decimal currently with .ToString(), it\'s accurate to like 15 decimal places, and since I\'m using it to represent dollars and ce

17条回答
  •  误落风尘
    2020-11-22 00:15

    You can use system.globalization to format a number in any required format.

    For example:

    system.globalization.cultureinfo ci = new system.globalization.cultureinfo("en-ca");
    

    If you have a decimal d = 1.2300000 and you need to trim it to 2 decimal places then it can be printed like this d.Tostring("F2",ci); where F2 is string formating to 2 decimal places and ci is the locale or cultureinfo.

    for more info check this link
    http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

提交回复
热议问题