无法将字符串解析为在C#中浮动

时间:2015-12-03 13:40:57

标签: c# .net string floating-point

我想将以下字符串转换为"5,28"float号码,我使用了这段代码,但是我得到了错误的结果。我也将设备语言设置为法语。

有什么我想念的吗?我试图将字符串转换为不同的文化,如CultureInfo("en-US"),但仍然无效。

bool result = float.TryParse("5,28", NumberStyles.Float,
                             CultureInfo.InvariantCulture.NumberFormat, out number); 

2 个答案:

答案 0 :(得分:3)

InvariantCulture使用.作为NumberDecimalSeparator而非,

由于您被迫使用Float样式,此样式在分隔符样式中仅包含 AllowDecimalPoint,您的方法认为此,是小数点分隔符但是InvariantCulture不使用它。这就是你获得例外的原因。

您可以做一些事情。一个选项可以是Clone InvariantCulture,将NumberDecimalSeparator属性设置为,,并在TryParse方法中使用 克隆文化。

float f;
var clone = (CultureInfo)CultureInfo.InvariantCulture.Clone();
clone.NumberFormat.NumberDecimalSeparator = ",";
var result = float.TryParse("5,28", NumberStyles.Float, clone, out f); // true

或者,您可以使用已经具有, {@ 1}} {@ 1}}文化的文化。 1

NumberDecimalSeparator

1 :因为我来自土耳其:)

答案 1 :(得分:1)

print("Please answer 'yes' or 'no' to all questions. If you write something else instead of yes or no then your solution might not come out correctly.") solution = "Keep holding the power on button until the screen is on. If it doesn't turn on, contact your phone provider to get a replacement." solution2 = "Charge your phone fully and switch it on." solution3 = "Delete some apps, you need a minimum of at least 5 GB of free memory to run the phone correcly.\nYou are probably out of memory." sol4 = "Take out everything from the phone and put it in a bag of rice for 24-36 hours to let the rice absorb the water." sol5 = "Reset your phone." sol6 = "You need a screen replacement. Get it fixed!" sol7 = "You need to get your buttons replaced!" sol8 = "Get a screen replacement or contact your phone provider to get a phone replacement." sol9 = "You need to update your phone software and apps." sol10 = "You dont need to do anything. Your phone doesnt have any problems." sol11 = "Please update your phone software." if input("Did you buy your phone recently? ") == 'yes': if input("Did your drop your phone? ") == 'yes': if input("Did it become wet when you dropped it? ") == 'yes': print(sol4) else: print(sol5) else: if input("Has your phone ever been to slow?" ) == 'yes': print(sol5) else: if input("Have you got more than 30 apps?? ") == 'yes': print(solution3) else: if input("Is your phone older than two years?") == 'yes': print(no_warranty) else: print(warranty) else: print(warranty) 未解析的原因是不变文化使用小数点View content = (LinearLayout)findViewById(R.id.linearLayout_QrCode); content.setDrawingCacheEnabled(true); Bitmap bitmap = content.getDrawingCache(); File file = new File("/sdcard/" + yourimagename + ".png"); try { if (!file.exists()) { file.createNewFile(); } FileOutputStream ostream = new FileOutputStream(file); bitmap.compress(CompressFormat.PNG, 100, ostream); ostream.close(); content.invalidate(); } catch (Exception e) { e.printStackTrace(); } finally { content.setDrawingCacheEnabled(false); } ,而不是小数点逗号。

要解决此问题,您可以使用点替换逗号,例如

5,28

或将.替换为使用逗号代替点的文化:

bool result=float.TryParse(
    "5.28"
,   NumberStyles.Float
,   CultureInfo.InvariantCulture.NumberFormat
,   out number);

Demo.