我在文本框这个数字“84,8441546842904”如何在84,8或84,84转换按钮点击事件删除电话号码?从一个文本框
Q
从一个文本框
0
A
回答
3
如果这你的意思是你要的价值和它全面解析到一定的小数位数:
double value = Math.Round(double.Parse(textbox.Text), 2);
将解析文本,并将其轮为2位小数。您可能需要解析考虑到当地文化的数字格式时使用System.Globalization.CultureInfo对象。
+0
非常感谢!快捷方便! – jolly
1
它几乎看起来像你想的数量修剪1或2精度(不是“”在一些国家,如美国的“”用?)。如果这是你以后,你可以用Double.Parse将其转换为Double,然后看看到字符串格式选项描述here对其进行格式化回文本框。
0
double i = 0;
if (double.TryParse(tbxNumber.Text,out i)) {
MessageBox.Show("number is " + i.ToString());
}
1
我使用这种功能来验证用户输入。
这种解决问题的方法也尊重用户的文化数字格式!
namespace Your_App_Namespace
{
public static class Globals
{
public static double safeval = 0; // variable to save former value!
public static bool isPositiveNumeric(string strval, System.Globalization.NumberStyles NumberStyle)
// checking if string strval contains positive number in USER CULTURE NUMBER FORMAT!
{
double result;
boolean test;
if (strval.Contains("-")) test = false;
else test = Double.TryParse(strval, NumberStyle, System.Globalization.CultureInfo.CurrentCulture, out result);
// if (test == false) MessageBox.Show("Not positive number!");
return test;
}
public static string numstr2string(string strval, string nofdec)
// conversion from numeric string into string in USER CULTURE NUMBER FORMAT!
// call example numstr2string("12.3456", "0.00") returns "12.34"
{
string retstr = 0.ToString(nofdec);
if (Globals.isPositiveNumeric(strval, System.Globalization.NumberStyles.Number)) retstr = double.Parse(strval).ToString(nofdec);
else retstr = Globals.safeval.ToString(nofdec);
return retstr;
}
public static string number2string(double numval, string nofdec)
// conversion from numeric value into string in USER CULTURE NUMBER FORMAT!
// call example number2string(12.3456, "0.00") returns "12.34"
{
string retstr = 0.ToString(nofdec);
if (Globals.isPositiveNumeric(numval.ToString(), System.Globalization.NumberStyles.Number)) retstr = numval.ToString(nofdec);
else retstr = Globals.safeval.ToString(nofdec);
return retstr;
}
}
// Other Your_App_Namespace content
}
// This is the way how to use those functions
// function to call when TextBox GotFocus
private void textbox_clear(object sender, System.Windows.RoutedEventArgs e)
{
TextBox txtbox = e.OriginalSource as TextBox;
// save original value
Globals.safeval = double.Parse(txtbox.Text);
txtbox.Text = "";
}
// function to call when TextBox LostFocus
private void textbox_change(object sender, System.Windows.RoutedEventArgs e)
{
TextBox txtbox = e.OriginalSource as TextBox;
// text from textbox into sting with checking and string format
txtbox.Text = Globals.numstr2string(txtbox.Text, "0.00");
}
相关问题
- 1. 从一个文本框
- 2. C#winforms:将文本从一个文本框移动到另一个文本框
- 3. 将“输入”从文本框传递到另一个文本框
- 4. Jquery +从gridview中的另一个文本框更新一个文本框
- 5. 一个文本框
- 6. 从一个文本框复制一个输入到另一个
- 7. 从一个页面加载数据文本框到另一页文本框?
- 8. 复制文本从一个文本框到另一个有一些限制
- 9. 使用javascript仅将文本从一个文本框复制到另一个文本框
- 10. C#按钮,将文本从一个文本框添加到另一个基于SQL查询的文本框
- 11. 节约multilined文本从一个文本框在vb.net
- 12. 附加一个文本框的值在第一次文本框
- 13. 如何将文本从一个文本框用一个按钮点击
- 14. 如何停止从下一个文本视图跳到下一个文本框?
- 15. 有一个文本框
- 16. javascript - unfocu一个文本框
- 17. 到一个文本框
- 18. 当一个文本框
- 19. 在一个文本框
- 20. 到一个文本框
- 21. RegEx.Replace在一个文本框
- 22. 复制值从一个文本框在一个特定的行
- 23. 填充从一个文本框的值到另一个
- 24. 复制值从一个文本框到另一个
- 25. jquery触发从另一个文本框中的文本框的关键事件
- 26. 从javascript的数组文本框并发送到另一个文本框
- 27. 从文本框
- 28. 从文本框
- 29. 从文本框
- 30. 当文档准备好时,将数据从一个文本框复制到另一个文本框
使用验证事件。和decimal.TryParse + decimal.ToString(“N2”)重新分配Text属性。你会免费获得舍入。 –
@HansPassant,但用户应该小心这种方法:ToString中的四舍五入与Math.Round中的四舍五入略有不同,默认为MidpointRounding.AwayFromZero,而AFAIK则无法指定不同的行为。 – phoog
是的,Math.Round得到了错误,其余的.NET框架正确。只有帐户想要明确地将其错误舍入。 –