C#蒙上了双变量为十进制 [英] C# cast a double variable to decimal

查看:140
本文介绍了C#蒙上了双变量为十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这将似乎是一个愚蠢的问题,在一定,但我需要投双为十进制货币来使用。哪里并购走

 小数dtot =(十进制)(doubleTotal)?; 


解决方案

您只使用中号为数字文字,当你施展它只是:

 小数dtot =(十进制)doubleTotal; 

请注意,一个浮点数不适合保持一个精确值,因此,如果您先加号在一起,然后转换为小数你可能会舍入误差。您可能希望将它们放在一起之前的数字转换为小数,或确认该数字不摆在首位浮点数。


This will seem like a silly question to some but I need to cast a double to decimal to use as currency. Where does the M go?

decimal dtot = (decimal)(doubleTotal);

解决方案

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.

这篇关于C#蒙上了双变量为十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆