C# 将double转换为int。。我可以删除点后的数字吗?

C# 将double转换为int。。我可以删除点后的数字吗?,c#,C#,我正在尝试从包含双数字(如“1.1”)的文本框转换项目。是否有一种方法可以将其格式化,以便删除“.1”并将其分配给变量“points” 是否有一种方法可以转换文本框“txtTotal”中的项目,该文本框将包含“1.1”,将其格式化为保存点之前的数字,然后将其分配到points变量,然后points将输出“1” 谢谢你的帮助 如果只想删除小数点后的所有数字,请使用Math.Truncate() 如果只想删除小数点后的所有数字,请使用Math.Truncate() 您可以尝试拆分小数点上的文本,然后

我正在尝试从包含双数字(如“1.1”)的文本框转换项目。是否有一种方法可以将其格式化,以便删除“.1”并将其分配给变量“points”

是否有一种方法可以转换文本框“txtTotal”中的项目,该文本框将包含“1.1”,将其格式化为保存点之前的数字,然后将其分配到points变量,然后points将输出“1”


谢谢你的帮助

如果只想删除小数点后的所有数字,请使用
Math.Truncate()


如果只想删除小数点后的所有数字,请使用
Math.Truncate()


您可以尝试
拆分小数点上的文本,然后使用
TrimStart
从第一个数组索引中删除
,我将使用
int.TryParse
检查输出是否有效

像这样:

int points;
txtTotal.Text = string.Format("£{0:0}",txtTotal.Text);
if(int.TryParse((txtTotal.Text.Split('.')[0].TrimStart('£')),out points))
   MessageBox.Show(points.ToString());

您可以尝试
拆分小数点上的文本,然后使用
TrimStart
从第一个数组索引中删除
,我将使用
int.TryParse
检查输出是否有效

像这样:

int points;
txtTotal.Text = string.Format("£{0:0}",txtTotal.Text);
if(int.TryParse((txtTotal.Text.Split('.')[0].TrimStart('£')),out points))
   MessageBox.Show(points.ToString());

看起来您正试图使用货币的字符串格式提取浮点(
double
)值的小数部分

我会这样做:

using System.Globalization; // For NumberStyles enum

var currencyString = txtTotal.Text;

// Parse the TextBox.Text as a currency value.    
double value;
var parsedSuccesfully = double.TryParse(currencyString,
                                        NumberStyles.Currency, 
                                        null,
                                        out value);

// TODO: Handle parsing errors here.

var wholePounds = Math.Truncate(value);
var fractionalPounds = (value - wholePounds);

// Get the whole and fractional amounts as integer values.
var wholePoundsInteger = (int)wholePounds;
var fractionalPoundsInteger = (int)(fractionalPounds * 1000.0); // 3 decimal places

看起来您正试图使用货币的字符串格式提取浮点(
double
)值的小数部分

我会这样做:

using System.Globalization; // For NumberStyles enum

var currencyString = txtTotal.Text;

// Parse the TextBox.Text as a currency value.    
double value;
var parsedSuccesfully = double.TryParse(currencyString,
                                        NumberStyles.Currency, 
                                        null,
                                        out value);

// TODO: Handle parsing errors here.

var wholePounds = Math.Truncate(value);
var fractionalPounds = (value - wholePounds);

// Get the whole and fractional amounts as integer values.
var wholePoundsInteger = (int)wholePounds;
var fractionalPoundsInteger = (int)(fractionalPounds * 1000.0); // 3 decimal places

如果您只想在
消息框中显示它
,可以将其作为字符串处理:

string points = txtTotal.Text;
points = points.Substring(0, points.IndexOf("."));
MessageBox.Show(points);

如果您只想在
消息框中显示它
,可以将其作为字符串处理:

string points = txtTotal.Text;
points = points.Substring(0, points.IndexOf("."));
MessageBox.Show(points);

如果文本本身为货币格式(磅,从外观上看),则应首先获取原始字符串,并通过指定货币的NumberStyle和适当的区域性(例如,对于en GB)将其转换为十进制:

最后,使用数学方法截断(或舍入,如果需要舍入):

int finalValue = Math.Truncate(currencyValue);
如果它不是货币格式,只是普通的double格式,那么对double进行更直接的解析就足够了:

double doubleValue = Double.Parse(txtTotal.Text);
int finalValue = Math.Truncate(doubleValue);

如果格式不一致,可能值得使用
TryParse
方法(而不是直接
Parse
)首先处理任何解析问题。

如果文本本身是货币格式(从外观上看是英镑),您应该首先获取原始字符串,并通过指定货币的NumberStyle和适当的区域性(例如,对于en GB)将其转换为十进制:

最后,使用数学方法截断(或舍入,如果需要舍入):

int finalValue = Math.Truncate(currencyValue);
如果它不是货币格式,只是普通的double格式,那么对double进行更直接的解析就足够了:

double doubleValue = Double.Parse(txtTotal.Text);
int finalValue = Math.Truncate(doubleValue);

如果格式不一致,可以使用
TryParse
方法(而不是直接
Parse
)首先处理任何解析问题。

您的问题不清楚。你能指定你的输入值和期望的输出值吗?
Convert.ToInt32
(或者简单地转换为
int
)有什么问题吗?我试着让它更清楚,tilak,john,如果有意义的话,文本框内的信息将包含一个双精度字符串:/you trusted
MessageBox.Show(points)
?您的问题不清楚。你能指定你的输入值和期望的输出值吗?
Convert.ToInt32
(或者简单地转换为
int
)有什么问题吗?我试着让它更清楚,tilak,john,如果有意义的话,文本框中的信息将包含一个双精度字符串:/you试过
消息框。Show(points)
?或Math.Round()取决于场景是的,但是如果数字保存在文本框中,我该怎么做?@Bunion double d=double.Parse(textbox.text);->int x=(int)Math.Truncate(d);如果
textbox.Text
不是数字,最好使用
Double.TryParse
。更好的是,根据场景使用numericUpDown控件或Math.Round()。是的,但是如果数字保存在文本框中,我该如何做?@Bunion Double d=Double.Parse(textbox.Text);->int x=(int)Math.Truncate(d);如果
textbox.Text
不是数字,最好使用
Double.TryParse
。最好使用numericUpDown控件。