C#中的decimal如何保留两位小数???

xingxingsuhuan 2010-04-22 08:27:16
问题是这样:
现在 parame[5].Value 是从数据库返回的output参数带有两位小数的值,如:46.28
我这如果以如下方式接受的话,得到的money1是46,是四舍五入后的,怎么样得到确切的46.28呢??
decimal money1 = (decimal)parame[5].Value;
...全文
91688 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
shenchen1212 2012-05-02
  • 打赏
  • 举报
回复
要多多请教你们 像你们询问请教 请多指教
shenchen1212 2012-05-02
  • 打赏
  • 举报
回复
谢啦 非常感谢
wy99love520 2010-09-26
  • 打赏
  • 举报
回复
我叉完全就是乱搞啥。没一个正确的。。。。。。。。。。。。。。。。。
xingxingsuhuan 2010-04-24
  • 打赏
  • 举报
回复
看了这个帖子就解决了问题
http://www.cnblogs.com/terryding/archive/2009/03/26/1060938.html
  • 打赏
  • 举报
回复
Console.WriteLine(你的变量.ToString("#0.00"));
tosting()方法里有很多转换格式的。
peterb 2010-04-23
  • 打赏
  • 举报
回复
参数parame[5]你设置其精度值了没有
Peter200694013 2010-04-23
  • 打赏
  • 举报
回复
decimal money1 = Decimal.Parse(parame[5].Value.ToString("#0.00"));
Peter200694013 2010-04-23
  • 打赏
  • 举报
回复
decimal d = 46.28111m;
Console.WriteLine(d.ToString("#0.00"));
q107770540 2010-04-23
  • 打赏
  • 举报
回复
不错,,学习了
shixiujin 2010-04-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zsw19909001 的回复:]
或者这种方法,不过要转换数据类型
decimal a = 46.28111;
string result=a.ToString("#0.00");
输出result=46.28
[/Quote]
学习了,支持!
xrongzhen 2010-04-23
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 xingxingsuhuan 的回复:]
真惊了,
没一个对的
[/Quote]

3楼的

double d=45.123456;
MessageBox.Show(double.Parse(String.Format("{0:N}", d)).ToString());
xingxingsuhuan 2010-04-23
  • 打赏
  • 举报
回复
先去试试在去结贴
xingxingsuhuan 2010-04-23
  • 打赏
  • 举报
回复
真惊了,
没一个对的
xingxingsuhuan 2010-04-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ly302 的回复:]
把你得到的值,替换那个d就可以
[/Quote]
decimal d = 46.28111;
在这句话时后面的两位就已经没有了
xingxingsuhuan 2010-04-23
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 peter200694013 的回复:]
decimal money1 = Decimal.Parse(parame[5].Value.ToString("#0.00"));
[/Quote]
“ToString”方法没有采用“1”个参数的重载
「已注销」 2010-04-22
  • 打赏
  • 举报
回复
或者这种方法,不过要转换数据类型
decimal a = 46.28111;
string result=a.ToString("#0.00");
输出result=46.28
wuyq11 2010-04-22
  • 打赏
  • 举报
回复
double d;
double.Parse(String.Format("{0:N}",d));

double x = double.Parse("");
double y = x % 0.01;
double z = x - y;

皇城龙三 2010-04-22
  • 打赏
  • 举报
回复
把你得到的值,替换那个d就可以
皇城龙三 2010-04-22
  • 打赏
  • 举报
回复
decimal d = 46.28111;

string dStr = Math.Round( d,2 ).ToString();

结果:dStr == "46.28"

111,131

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧