Click here to Skip to main content
15,901,949 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.


here the code ,

C#
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!
Posted
Updated 12-Oct-15 19:27pm
v2
Comments
Member 12003400 13-Oct-15 5:01am    
In case of totq is 1,,no chance of error,as you already put a condition if (totq.Contains(".")) and if the value is 1,it wont go further so your code will not execute. Try to give detail of your question.

Hi,
You can handle the exception by making sure that your value having more than 3 digit other wise no need to substring the values.
C#
if (totq.Contains(".") && totq.Length>3)
            {
                totq = totq.Substring(0,3);
            }
 
Share this answer
 
You can send 1 as 1.0, 2 as 2.0, and so on. Use this:
decimal.Round(totq, 1, MidpointRounding.AwayFromZero);

Let me know if it works :)

-KR
 
Share this answer
 
Comments
Member 11543226 13-Oct-15 1:45am    
the value is unknown to me its comes out from database it should be atleast four digit ,
is their any regex expression for this .
Krunal Rohit 13-Oct-15 2:13am    
Did you try this ?

-KR
Member 11543226 13-Oct-15 2:58am    
I found answer by using decimal class . as

decimal due = (decimal)Math.Round(Convert.ToDecimal(totq), 4);
totq = due.ToString("0.000");
Member 11543226 13-Oct-15 2:58am    
thanks for help

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