我在文本框這個數字「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框架正確。只有帳戶想要明確地將其錯誤舍入。 –