-5

I have decimal values in the database. I want to show the decimal value in it. How to convert decimal values into it.

int vatprice;
dt.Load(cmd.ExecuteReader());
vatprice = Convert.ToInt32(dt.Rows[0][5].ToString());//getting error here
andreasnico
  • 1,478
  • 14
  • 23
krishna mohan
  • 905
  • 4
  • 20
  • 38
  • 3
    What is the error message? What is the value of `dt.Rows[0][5].ToString()` exactly? Why do you get it as an int if it is decimal? – Soner Gönül Mar 01 '16 at 12:45
  • 1
    Hey, Please ask question here with more clarification. What exception you are getting and also what is value of dt.Rows[0][5] . – KP Chundawat Mar 01 '16 at 12:50
  • vatprice = Convert.ToDecimal(dt.Rows[0][5].ToString());// how to restrict decimal upto max of two decimal after decimal – krishna mohan Mar 01 '16 at 12:51
  • You declare `vatprice` as `int` and now you want to assign a `decimal` value to it with a maximum of 2 positions after the decimal point? – Ferhat Sayan Mar 01 '16 at 12:55
  • i used this , totalPriceWithVAT = Math.Round(totalPriceWithVAT, 2);, but getting oonly 1 decimal value – krishna mohan Mar 01 '16 at 12:56
  • then for rounding you can try http://stackoverflow.com/questions/6951335/using-string-format-to-show-decimal-upto-2-places-or-simple-integer – Claudius Mar 01 '16 at 12:59
  • thanks u guys i got my answer. i changed into to decimal and used - vatprice = Math.Round(vatprice, 2); – krishna mohan Mar 01 '16 at 13:00
  • that's probably because the decimal part ends with a 0, use string.format() as suggested by @Claudius – Mirko Bellabarba Mar 01 '16 at 13:00

1 Answers1

1

As per your requirement, round decimal up to 2 place using following way :

1) Use Math.Round().

 var vatprice= Convert.ToDecimal(Math.Round(dt.Rows[0][5], 2));

2) second way is

var vatprice=Convert.ToDecimal(dt.Rows[0][5].ToString ("#.##"));
KP Chundawat
  • 935
  • 9
  • 25