转换为两位小数 [英] Convert to two decimal places

查看:115
本文介绍了转换为两位小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在使用vb.net。我写了一个插入货币详细信息的代码。我想把两位小数后的货币值换成
。我怎么能这样做。

(插入3个货币值的代码)



 getdetails.Parameters。添加(  @ cash_buy,SqlDbType.Float).Value = txt_cash_buy.Text 
getdetails .Parameters.Add( @ cash_sell,SqlDbType.Float).Value = txt_cash_sell.Text
getdetails.Parameters.Add( @ dd_sell,SqlDbType.Float).Value = txt_dd_sell。文本

解决方案

Parse 方法十进制在这里解释

Decimal.Parse Method(String) [ ^ ]

Math class e的 Round 方法xplained here / b
Math.Round方法(十进制,Int32,MidpointRounding) [ ^ ]

可以按如下方式使用:

 getdetails.Parameters.Add(  @dd_sell,SqlDbType.Float).Value = 
Math.Round( decimal .Parse(txt_dd_sell.Text ), 2 ,MidpointRounding.AwayFromZero)



默认情况下回合方法使用 MidpointRounding.ToEven 枚举值,这意味着如果要舍入的值是 2.555 它将四舍五入为 2.56 ,如果它是 2.565 ,那么它也将四舍五入为 2.56 as 5 是中点。如果使用 MidpointRounding.AwayFromZero ,则上述值将分别四舍五入为 2.56和2.57


hi,



这是有效的

  double  val = Convert.ToDouble(YourTextBox.Text); 
val = Math.Round(val, 2 );


hi

i am using vb.net. i wrote a code for inserting currency details. i want to round
the currency value after two decimal. how can i do that.
(code for inserting 3 currency values)

getdetails.Parameters.Add("@cash_buy", SqlDbType.Float).Value = txt_cash_buy.Text
            getdetails.Parameters.Add("@cash_sell", SqlDbType.Float).Value = txt_cash_sell.Text
            getdetails.Parameters.Add("@dd_sell", SqlDbType.Float).Value = txt_dd_sell.Text

解决方案

The Parse method of Decimal explained here
Decimal.Parse Method (String)[^]
and the Round method of Math class explained here
Math.Round Method (Decimal, Int32, MidpointRounding)[^]
can be used as follows:

getdetails.Parameters.Add("@dd_sell", SqlDbType.Float).Value =
Math.Round(decimal.Parse(txt_dd_sell.Text),2,MidpointRounding.AwayFromZero)


By default the Round method uses MidpointRounding.ToEven enumeration value, which means that if the value to be rounded is 2.555 it will be rounded off to 2.56 and if it is 2.565 then also it will be rounded off to 2.56 as 5 is the mid point. If the MidpointRounding.AwayFromZero is used then the above values will be rounded off to 2.56 and 2.57 respectively.


hi,

this works

double val = Convert.ToDouble(YourTextBox.Text);
val = Math.Round(val, 2);


这篇关于转换为两位小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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