You must Sign In to post a response.
  • Category: ASP.NET

    How to handle decimal point value?

    I am little bit confuse here, that how to handle decimal point value as i have to receive values like 0.1 , 0.012334839493 , 0.00193999329 , 1 , 2 . when i get this values from database and i have to sent this values upto 4 digits , it is possible with values more than 4 digits but when value less than 4 digit (like 0.1 , 1 , 2 ) comes it gives error. it cannot handle that value , is there any way to handle this situation.

    foreach (DataRow row in table.Rows)
    {
    string stock = row["stocksum_id"].ToString();
    string product = row["prod_name"].ToString();
    string packing = row["conversion_factor"].ToString();
    string mfg = row["manufacturer_id"].ToString();
    string batch = row["batchno"].ToString();
    string expiry = row["expiry_date"].ToString().Substring(0, 10);
    string mrp = row["mrp"].ToString();
    string totq = row["current_stock"].ToString();
    string[] mrpvalue = row["purchase_price"].ToString().Split('.');
    string barcode = row["scan_code"].ToString();
    string prodGrade = row["prod_grade"].ToString();

    if (totq.Contains("."))
    {
    totq = totq.Substring(0, 3);
    }

    when totq receive value 0.1 or 1 it gives error.

    Thanks!
  • #762826

    Hai Priyanka Barwad,
    You can format the incoming values and then you can use substring as:

    Console.WriteLine(String.Format("{0:0.0000}", 0.1));
    Console.WriteLine(String.Format("{0:0.0000}", 0.012334839493));
    Console.WriteLine(String.Format("{0:0.0000}", 0.00193999329));
    Console.WriteLine(String.Format("{0:0.0000}", 1));
    Console.WriteLine(String.Format("{0:0.0000}", 2));

    With this, you will get the result as:

    0.1000
    0.0123
    0.0019
    1.0000
    2.0000

    Now you can use your code as below:

    totq = string.Format("{0:0.0000}", totq); // convert the totq string value to decimal or double
    if (totq.Contains("."))
    {
    ..
    }

    Hope it will be helpful to you.


    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


Sign In to post your comments