Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)

VB
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
Posted
Updated 16-Aug-20 21:08pm
Comments
VJ Reddy 4-May-12 6:41am    
The solution 1 is good. But see solution 2 regarding the mid point rounding.
Thank you.

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:
VB
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.
 
Share this answer
 
v3
Comments
Maciej Los 4-May-12 6:37am    
Good answer, my 5!
VJ Reddy 4-May-12 6:40am    
Thank you, losmac.
hi,

this works
C#
double val = Convert.ToDouble(YourTextBox.Text);
val = Math.Round(val, 2);
 
Share this answer
 
Comments
afsal.mp 4-May-12 6:08am    
Thank you tanweer akthar.. It works
Maciej Los 4-May-12 6:37am    
Good answer, my 5!
VJ Reddy 4-May-12 6:40am    
Good answer. 5!
tanweer 4-May-12 7:36am    
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900