如何转换long到int在.net? [英] How to convert long to int in .net?

查看:110
本文介绍了如何转换long到int在.net?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发window phone 7应用程序在silverlight。我是新的窗口手机7应用程序。我有String格式的长值如下

  String Am = AmountTextBox.Text.ToString()

上述代码中的AmountTextBox.Text.ToString()是字符串格式的长整型值。我想在我的应用程序中存储一个15位数字的间隔值。



我找到以下链接进行转换。



我可以将long转换为int吗?



如何将字符串格式的long值转换为int?您能否提供任何我可以解决上述问题的代码或链接?

解决方案

您不能存储15位整数,因为最大值为


$ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $

您可以使用 TryParse()从yout用户输入获取 long -Value:

  String Am = AmountTextBox.Text.ToString(); 
long l;
Int64.TryParse(Am,out l);

如果文本可以返回 false



否则,转换<$ c $> c> long int 是一个很容易的

  int i =(int)yourLongValue;如果您对弃用MSB和使用LSB感到高兴,请


I am developing window phone 7 application in silverlight. I am new to the window phone 7 application. I have the long value in String format as follows

String Am = AmountTextBox.Text.ToString() 

The AmountTextBox.Text.ToString() in the above code is long value which is in string format. I want to store a 15 digit inter value in my application.

I found the following link for conversion.

Can I convert long to int?

How should I convert a long value which is in string format to int ? Can you please provide me any code or link through which I can resolve the above issue ? If I am doing anything wrong then please guide me.

解决方案

You can't store a 15 digit integer since the maximum value for an integer is 2,147,483,647.

What's wrong with a long-Value?

You could use TryParse() to get the long-Value from yout user input:

String Am = AmountTextBox.Text.ToString();
long l;
Int64.TryParse(Am, out l);

It will return false if the text can't be converted to long, so it's pretty safe to use.

Otherwise, converting a long to int is a easy as

int i = (int)yourLongValue;

if your happy with discarding MSBs and taking LSBs.

这篇关于如何转换long到int在.net?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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