字符串0,1234到整数 [英] String 0, 1234 to integer

查看:197
本文介绍了字符串0,1234到整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将字符串0,1234转换为可以存储在具有INT字段的SQL表中的整数



我尝试过:



how is it possible to convert a string 0,1234 to an integer that I can store in an SQL table that has an INT as field

What I have tried:

_sAmount = dr[13].ToString().Trim().Replace(",",".");
                    _Amount = Convert.ToInt32(_sAmount,4);

推荐答案

用点替换逗号不起作用:这不会创建一个整数 - 它会创建一个浮点数:0.1234,它不会解析为整数。



你可以使用 double.TryParse 而不是 Convert.ToInt32 然后将其转换为整数,但这只会给你零,因为.1234部分将被丢弃。



我会查看我的数据源,并尝试确切地知道它应该保留的内容:为什么您的DataTable列包含数字0,1234?那应该是两个逗号分隔的数字吗?还是一个严重分开的数字01,234?或者是什么?你甚至有合适的专栏吗?我们可以告诉你,也许你也不能!



从数据开始,找出你拥有的东西:只相信它并盲目转换它给你带来巨大的问题。
Replacing the comma with a dot won't work: that doesn't create an integer - it creates a a floating point number: "0.1234" which will not parse as an integer.

You could use double.TryParse instead of Convert.ToInt32 and then cast that to an integer, but that will just give you zero, as the .1234 part will be discarded.

I'd look at my data source, and try to work out exactly what it is supposed to hold: why does your DataTable column hold numbers like "0,1234"? Is that supposed to be two comma delimited numbers? Or a single, badly separated number "01,234"? Or what? Do you even have the right column? We can;t tell, and probably you can't either!

Start with the data, and work out what you have: just trusting it and converting it blindly will give you huge problems down the line.


如果它是0.1234那么一个int将不够精确



尝试加倍。 parse(s)
If it's 0.1234 then an int won't be precise enough

Try double.parse(s)


有效但我也有数字



9999.9999和-9999.9999



如此负面且在逗号后面......

如何将该字符串转换为sql数据库。

在我使用的数据库中数字(10,5)巫婆工作,但如何将它们从我的代码c#从字符串转换为数字?



多米尼克
that works but also I have numbers between

9999.9999 and -9999.9999

so negative and after the comma numbers..
how to convert that string into a sql data base.
In the database I use Numeric (10,5) witch works but how to convert them from my code c# from a string to a numeric??

dominick


这篇关于字符串0,1234到整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆