简体   繁体   中英

decimal.Parse method

I just found a very weird Bug in my c# code,

so there is a next:

when i´m trying to cast a object{string} to decimal like this :

item.ValiaFundo = row.IsNull("PL_FUNDO") ? 0 : decimal.Parse(row["PL_FUNDO"].ToString());

with the value of row = "2488.1987", it returns me 2488.1987 as supposed, but for some users returned value is 24881987.

i fixed it changing a type of object my database return to object{decimal}, like this :

item.ValiaFundo = row.IsNull("PL_FUNDO") ? 0 : (decimal)row["PL_FUNDO"];

that casts a value well for any user, and i just want to understand why that bug happens? What cause it? Does anyone have some ideas?

PS: server and users regional settings is the same for both cases.

But, seems that data coming from server is already decimal in type.

It is a bad practice to go decimal --> string --> decimal.

As Jon Skeet pointed out (his answer disappeared) in some cultures comma is a decimal point. So when doing ToString() be careful about culture used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM