English 中文(简体)
如何将字符串转换为整数?
原标题:How to convert string to int?
  • 时间:2012-05-22 11:22:59
  •  标签:
  • asp.net

How to convert string to int?I know convert.ToInt32 is used.But it fail.Error is input string is not in proper format. String s1 = "12.00"

最佳回答

err或 is because 字符串是 12. 00 ,因为字符串是 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

首次转换字符串为整数的两倍

int a = Convert.ToInt32(Convert.ToDouble("12.00"));

如果您只想要整数部分, 而不是

string s= "12.00";
string[] w或ds = s.Split( . );
int a = Convert.ToInt32(w或ds[0]);

还检查在 SO 上已回答的线索 : < a href=" https://stackoverflow.com/ questions/4975613/ c- sharp- convert- string- decimal- to-int > > C# 转换字符串十进制到 Int

问题回答

我喜欢这样的假设,即小数分隔符始终是一个点( . )。您最好使用 < a href="http://msdn.microsoft.com/en-us/library/system.globalization.culationinfo.invariantculture.aspx" rel="nofollow"\\\"InvariantCulture > ,其中含有一个数字格式,明确指定点为小数分隔符:

Convert.ToInt32(Convert.ToDouble("12.00", CultureInfo.InvariantCulture));

为了澄清:。当我在带有荷兰文化的PC上运行时,没有指定文化信息时,它会使用系统默认值(comma)并返回 1200 ,忽略点。

虽然它并不直接影响你的问题,但它是无法强调得足够的问题。

“ 12. 00” 是小数数, 不是整数。 整数组没有分数部分 。 在调用 < code> 之前, 请先使用 < a href=" http:// msdn. microsoft. com/ en- us/ library/ system. convert. to twonder. aspx" rel = “ nofollow”\\ code > convert. ToDouble 或类似的, 以获得浮点号, 或者将字符小数部分( < code>. < / code > 和后续内容) 从小数部分( < code> Convert. Tot32

字符串“ 12. 00” 是一个双倍/ 十进制值。 使用双倍. parse () 或双倍. TryParse () 或 转换. Todouble () 。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!