sql类型浮点数、实数、小数?
在我的数据库中,我有一个产品价格列 我把它设置为 float
,我的问题是如果我从我的 C# 应用程序开始保存它 作为 10.50 .. 在查询中它返回 10,50 并且如果我更新我会收到错误 10,50 无法转换为 float
...或者类似的东西.. 如果我将其保存为十进制
,在sql管理内的查询中..就可以了.. 但在我的 C# 应用程序中...我遇到了同样的错误.. 10.50 返回为 10,50 我不知道为什么,以及如何解决它..我独特的解决方案保存了它 作为varchar
...
well in my database i had a colum for 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
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是某种本地化问题。
10,50
是“欧洲”书写方式“十半”。如果您从select
语句中得到该信息,那么您的数据库可能配置不正确。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 yourselect
statements then your database is probably configured incorrectly.一般来说,您应该在整个图层中使用相同的类型。因此,如果数据库中的基础类型是 x,您也应该在 C# 中传递具有相同类型的数据。
您选择什么类型取决于您要存储的内容 - 您不应该仅仅为了让某些内容“工作”而切换类型。为此,以非数字类型(例如 varchar)存储数字数据很快就会给您带来麻烦。很高兴您打开这个问题来解决这个问题!
正如其他人奇迹般地推断的那样,您可能会遇到本地化问题。这是一个很好的例子,说明了为什么将数字存储为字符串是一个问题。如果您正确地接受用户输入的任何文化/本地化他们想要的(或您想要的),并将其放入数字类型变量中,那么其余的(与数据库对话)应该很容易。更重要的是,如果可以的话,你不应该在数据库中进行数字格式化——这些东西最好放在前端,更靠近用户。
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.
我认为您在 Windows 区域和语言中小数符号的设置是错误的。请将其设置为点并再次测试。
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.
是的,本地化。
也就是说,我认为你的照片存储在 SQLServer 中的“金钱”字段中(我假设你正在使用 SQLServer)。如果那是数据库中的浮点数,它将返回一个正常的小数点,而不是欧洲货币分隔符“,”。
修复:
首先在您的 C# 代码中请勿使用 FLOAT,除非您绝对需要浮点数。请改用十进制类型。这不仅是在这种情况下,而是在所有情况下。浮点数是二进制(基数为 2),而不是十进制(基数为 10),因此您在界面中看到的只是实际数字的十进制近似值。结果是
(1 == 1) 经常被评估为 false!
我自己也遇到过这个问题,如果您不知道这种情况会发生,那就太令人抓狂了。在 C# 中始终使用
decimal
而不是float
。好的,修复此问题后,请执行以下操作以获得正确的本地化:
请注意,“dbPriceDataField”必须是字符串,因此您可能必须在该数据库结果集的字段上执行“.ToString()”。
如果您最终必须处理该货币字段的其他“货币”方面(例如货币符号),请查看:http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.aspx
如果您需要更强大的错误处理,请输入十进制。Parse在一个try/catch,或者使用decimal.TryParse。
编辑 -
如果您知道数据库设置为哪种文化(实际上是国家/地区),您可以这样做:
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 offloat
in c#.Ok, after you've fixed that, then do this to get the right localization:
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:
我在我的网络应用程序中遇到了这样的问题......但我找到了解决方案,就像我在文本框中获取我的价格值一样。所以我附有数据库。因此,当您使用文本框附加数据库时...当您右键单击文本框并单击“编辑数据绑定”...时,您必须提供...像“绑定属性”中那样键入... {0:N2}
这仅适用于网络应用程序或网站...不适用于桌面应用程序...
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...