Cast a Double Variable to Decimal

后端 未结 5 732
没有蜡笔的小新
没有蜡笔的小新 2020-12-15 02:22

How does one cast a double to decimal which is used when doing currency development. Where does the M go?

decimal dtot          


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-15 02:44

    You only use the M for a numeric literal, when you cast it's just:

    decimal dtot = (decimal)doubleTotal;
    

    Note that a floating point number is not suited to keep an exact value, so if you first add numbers together and then convert to Decimal you may get rounding errors. You may want to convert the numbers to Decimal before adding them together, or make sure that the numbers aren't floating point numbers in the first place.

提交回复
热议问题