如何处理小数点值?
问题描述:
我在这里有点混淆,如何处理小数点值,因为我必须接收像0.1,0.012334839493,0.00193999329,1,2这样的值。当我从数据库中获取此值并且我必须将此值发送到4位数时,可能使用超过4位数的值,但是当值小于4位(如0.1,1,2)时会出现错误。它无法处理这个值,有没有办法处理这种情况。
这里代码,
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 ,
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);
}
当totq收到0.1或1的值时会出错。
谢谢!
when totq receive value 0.1 or 1 it gives error.
Thanks!
答
您可以通过确保您的价值来处理异常超过3位数其他方面不需要对值进行子串。
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.
if (totq.Contains(".") && totq.Length>3)
{
totq = totq.Substring(0,3);
}
您可以将1发送为1.0,2发送为2.0,依此类推。使用此:decimal.Round(totq,1,MidpointRounding.AwayFromZero);
让我知道它是否有效:)
-KR
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