[英]Converting string to double in C#
我有一個雙類型值的長字符串,由#
- value1#value2#value3#
等分隔
我將它拆分為字符串表。 然后,我想將此表中的每個元素都轉換為雙精度類型,但出現錯誤。 這里的類型轉換有什么問題?
string a = "52.8725945#18.69872650000002#50.9028073#14.971600200000012#51.260062#15.5859949000000662452.23862099999999#19.372202799999250800000045#51.7808372#19.474096499999973#";
string[] someArray = a.Split(new char[] { '#' });
for (int i = 0; i < someArray.Length; i++)
{
Console.WriteLine(someArray[i]); // correct value
Convert.ToDouble(someArray[i]); // error
}
有3個問題。
1) 小數點分隔符不正確
不同的文化使用不同的小數點分隔符(即,
和.
)。
如果更換.
,
它應該按預期工作:
Console.WriteLine(Convert.ToDouble("52,8725945"));
您可以使用重載方法解析雙打,該方法將文化作為第二個參數。 在這種情況下,您可以使用InvariantCulture
( 什么是不變文化),例如使用double.Parse
:
double.Parse("52.8725945", System.Globalization.CultureInfo.InvariantCulture);
您還應該看看double.TryParse
,您可以將它與許多選項一起使用,並且檢查您的字符串是否是有效的double
尤其有用。
2)你的雙打不正確
您的值之一不正確,因為它包含兩個點:
15.5859949000000662452.23862099999999
3)你的數組最后有一個空值,這是一個不正確的雙
您可以使用重載的Split
刪除空值:
string[] someArray = a.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);
添加一個類為 Public 並像 convertToInt32() 一樣輕松使用它
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Common
/// </summary>
public static class Common
{
public static double ConvertToDouble(string Value) {
if (Value == null) {
return 0;
}
else {
double OutVal;
double.TryParse(Value, out OutVal);
if (double.IsNaN(OutVal) || double.IsInfinity(OutVal)) {
return 0;
}
return OutVal;
}
}
}
然后調用函數
double DirectExpense = Common.ConvertToDouble(dr["DrAmount"].ToString());
大多數人已經嘗試回答您的問題。
如果還在調試,有沒有想過使用:
Double.TryParse(String, Double);
這將幫助您在進行實際解析之前首先確定每個字符串中有什么問題。
如果您有與文化相關的問題,您可以考慮使用:
Double.TryParse(String, NumberStyles, IFormatProvider, Double);
這個http://msdn.microsoft.com/en-us/library/system.double.tryparse.aspx有一個關於如何使用它們的很好的例子。
如果您需要很長的 Int64.TryParse,也可以使用: http : //msdn.microsoft.com/en-us/library/system.int64.tryparse.aspx
希望有幫助。
private double ConvertToDouble(string s)
{
char systemSeparator = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator[0];
double result = 0;
try
{
if (s != null)
if (!s.Contains(","))
result = double.Parse(s, CultureInfo.InvariantCulture);
else
result = Convert.ToDouble(s.Replace(".", systemSeparator.ToString()).Replace(",", systemSeparator.ToString()));
}
catch (Exception e)
{
try
{
result = Convert.ToDouble(s);
}
catch
{
try
{
result = Convert.ToDouble(s.Replace(",", ";").Replace(".", ",").Replace(";", "."));
}
catch {
throw new Exception("Wrong string-to-double format");
}
}
}
return result;
}
並成功通過的測試是:
Debug.Assert(ConvertToDouble("1.000.007") == 1000007.00);
Debug.Assert(ConvertToDouble("1.000.007,00") == 1000007.00);
Debug.Assert(ConvertToDouble("1.000,07") == 1000.07);
Debug.Assert(ConvertToDouble("1,000,007") == 1000007.00);
Debug.Assert(ConvertToDouble("1,000,000.07") == 1000000.07);
Debug.Assert(ConvertToDouble("1,007") == 1.007);
Debug.Assert(ConvertToDouble("1.07") == 1.07);
Debug.Assert(ConvertToDouble("1.007") == 1007.00);
Debug.Assert(ConvertToDouble("1.000.007E-08") == 0.07);
Debug.Assert(ConvertToDouble("1,000,007E-08") == 0.07);
你可以試試這個例子。 一個簡單的 C# 程序將字符串轉換為雙精度
class Calculations{
protected double length;
protected double height;
protected double width;
public void get_data(){
this.length = Convert.ToDouble(Console.ReadLine());
this.width = Convert.ToDouble(Console.ReadLine());
this.height = Convert.ToDouble(Console.ReadLine());
}
}
在您的字符串中,我看到: 15.5859949000000662452.23862099999999
這不是雙15.5859949000000662452.23862099999999
數(它有兩個小數點)。 也許這只是一個合法的輸入錯誤?
您可能還想弄清楚您的最后一個String
是否為空,並考慮這種情況。
public static double ConvertToDouble(string value)
{
if (value == null) return 0;
double.TryParse(value, out var val);
return double.IsNaN(val) || double.IsInfinity(val) ? 0 : val;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.