开发者

sql type float, real, decimal?

开发者 https://www.devze.com 2023-03-19 08:00 出处:网络
well in my database i had a colum f开发者_JS百科or price of one product i had it as float, my problem is if i saved it since my c# application

well in my database i had a colum f开发者_JS百科or price of one product i had it as float, my problem is if i saved it since my c# application as 10.50 .. in a query it returns 10,50 and if i update i get a error 10,50 cant convert to float ... or something so.. and if i saved it as decimal, in queries inside sql management .. are ok.. but in my c# application... i get the same error.. 10.50 retuns as 10,50 i dont know why, and how to solved it.. my unique solution is saved it as varchar...


That's a localisation problem of some sort. 10,50 is the "European" way of writing ten and a half. If you're getting that from your select statements then your database is probably configured incorrectly.


Generally speaking you should use the same type throughout your layers. So if the underlying types in the database are x, you should pass around those data with identical types in c#, too.

What type you choose depends on what you are storing--you shouldn't be switching around types just to get something to "work". To that end, storing numeric data in a non-numeric type (e.g. varchar) will come back to bite you very soon. It's good you've opened this question to fix that!

As others have miraculously inferred, you are likely running into a localization issue. This is a great example of why storing numbers as strings is a problem. If you properly accept user input in whatever culture/localization they want (or you want), and get it into a numeric-type variable, then the rest (talking to the DB) should be easy. More so, you should not do number formatting in the database if you can help it--that stuff is much better placed at the front end, closer to the users.


I think your setting in windows regional and language for decimal symbol is wrong.please set it to dot and again test it.


This may help out for temporary use but I wouldn't recommend it for permanent use:

Try making it so that just before you save the file, convert the number to a string, replace the commas with periods (From , to .) and then save it into the database as the string, hopefully it should see that it is in the correct format and turn it into what the database sees as "Decimal" or "Floating".

Hope this helps.


Yep, localization.

That said, I think your pice is being stored on a "money" field in SQLServer (I'm assuming it's SQLServer you're using). If that was a float in the DB, it would return it with a normal decimal point, and not the European money separator ",".

To fix: Fist DO NO USE FLOAT in your c# code, unless you absolutely require a floating point number. Use the decimal type instead. That's not just in this case, but in all cases. Floating point numbers are binary (base-2), not decimal (base-10), so what you see in the interface is only a decimal approximation of the actual number. The result is that frequently (1 == 1) evaluates as false!

I've run into that problem myself, and it's maddening if you don't know that can happen. Always use decimal instead of float in c#.

Ok, after you've fixed that, then do this to get the right localization:

using System.Globalization;
...

NumberFormatInfo ni = new NumberFormatInfo();
ni.CurrencyDecimalSeparator = ",";
decimal price = decimal.Parse(dbPriceDataField, ni);

Note that "dbPriceDataField" must be a string, so you may have to do a ".ToString()" on that db resultset's field.

If you end up having to handle other "money" aspects of that money field, like currency symbols, check out: http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.aspx

If you need more robust error handling, either put that decimal.Parse in a try/catch, or use decimal.TryParse.

EDIT --

If you know what culture (really, country), the db is set to, you can do this instead:

using System.Globalization;
...
CultureInfo ci = new CultureInfo("fr-FR"); // fr-FR being "french France"
decimal price = decimal.Parse(dbprice, ci.NumberFormat);


Such problems were faced by me in my Web Apps... but i found the solution like I was fetching my price value in textbox. So I was have database attached with that. So when you attached your database with textbox... When you right click textbox and click Edit DataBinding.... in that you have to provide.... type like in Bind Property..... {0:N2}

This will work only for web apps or websites... not for desktop applications...

0

精彩评论

暂无评论...
验证码 换一张
取 消