转换为两位小数 [英] Convert to two decimal places
问题描述
嗨
我正在使用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
as5
是中点。如果使用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
TheParse
method ofDecimal
explained here
Decimal.Parse Method (String)[^]
and theRound
method ofMath
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 theRound
method usesMidpointRounding.ToEven
enumeration value, which means that if the value to be rounded is2.555
it will be rounded off to2.56
and if it is2.565
then also it will be rounded off to2.56
as5
is the mid point. If theMidpointRounding.AwayFromZero
is used then the above values will be rounded off to2.56 and 2.57
respectively.
hi,
this works
double val = Convert.ToDouble(YourTextBox.Text); val = Math.Round(val, 2);
这篇关于转换为两位小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!