C# 仅转换特定值的有效方法
假设我有一个字符串数组:C# 仅转换特定值的有效方法,c#,.net,parsing,casting,rounding,C#,.net,Parsing,Casting,Rounding,假设我有一个字符串数组: string[] values = new[] { "1", "2", "3", "1.5", "56.5", "8" }; 假设我必须循环这些值并执行以下操作: 将其四舍五入到最近的偶数(如果仅为双精度) 四舍五入后,删除数字的小数部分 如果数字为负数,则删除该符号 string[]value=new[]{“1”、“2”、“3”、“1.5”、“56.5”、“8”}; for(int i=0;i
string[] values = new[] { "1", "2", "3", "1.5", "56.5", "8" };
假设我必须循环这些值并执行以下操作:
string[]value=new[]{“1”、“2”、“3”、“1.5”、“56.5”、“8”};
for(int i=0;i
这与这样做基本相同:
string[] values = new[] { "1", "2", "3", "1.5", "56.5", "8" };
for (int i = 0; i < values.Length; i++)
{
String strValue = values[j];
Double dblValue = Double.Parse(strValue);
Double dblRoundedValue = Double.Parse(dblValue);
Int32 intValue = (Int32)dblRoundedValue;
Int32 intAbsValue = Math.Abs(intValue);
String finalValue = intAbsValue.ToString();
file[i].Value = finalValue;
}
string[]value=new[]{“1”、“2”、“3”、“1.5”、“56.5”、“8”};
for(int i=0;i
这个数组中可能有超过一百万个值,那么有没有办法使这个过程更高效?这个操作本质上是可并行的(如果是这样的话)。一个
Parallel.ForEach
循环、并行Linq管道或类似的东西可以提高执行时间
string[] values = new[] { "1", "2", "3", "1.5", "56.5", "8" };
var file = values.AsParallel()
.Select(s => Double.Parse(s))
.Select(d => (int)Math.Round(d))
.Select(i => Math.Abs(i).ToString())
.ToArray();
这个操作本质上是可并行的(如果是这样的话)。一个
Parallel.ForEach
循环、并行Linq管道或类似的东西可以提高执行时间
string[] values = new[] { "1", "2", "3", "1.5", "56.5", "8" };
var file = values.AsParallel()
.Select(s => Double.Parse(s))
.Select(d => (int)Math.Round(d))
.Select(i => Math.Abs(i).ToString())
.ToArray();
假设您的意思是“偶数”,则表示“最近的整数”,而不是“2的整数倍”。
假设您的意思是从所有数字中删除负号,而不是仅从四舍五入的数字中删除负号
假设输入字符串总是有效的双格式,这合理吗?假设没有使用科学记数法格式化的数字,检查每个输入字符串的小数点,这比解析和测试小数部分要快。无小数点,无需四舍五入。假设您的意思是“偶数”表示“最近的整数”,而不是“2的整数倍”。
Double dblValue;
Double dblRoundedValue;
Int32 intValue;
Int32 intAbsValue;
String finalValue;
for (int i = 0; i < values.Length; i++)
{
strValue = values[j];
if (!Int32.TryParse(strValue, out intValue))
{
//dblValue = Double.Parse(strValue);
//dblRoundedValue = Double.Parse(dblValue);
//intValue = (Int32)dblRoundedValue;
intValue = (Int32)(Double.Parse(strValue));
}
//intValue = Math.Abs(intValue);
//finalValue = intValue .ToString();
file[i].Value = (Math.Abs(intValue)).ToString();
}
假设您的意思是从所有数字中删除负号,而不是仅从四舍五入的数字中删除负号
假设输入字符串总是有效的双格式,这合理吗?假设没有使用科学记数法格式化的数字,检查每个输入字符串的小数点,这比解析和测试小数部分要快。无小数点,无需四舍五入。双D左值;
Double dblValue;
Double dblRoundedValue;
Int32 intValue;
Int32 intAbsValue;
String finalValue;
for (int i = 0; i < values.Length; i++)
{
strValue = values[j];
if (!Int32.TryParse(strValue, out intValue))
{
//dblValue = Double.Parse(strValue);
//dblRoundedValue = Double.Parse(dblValue);
//intValue = (Int32)dblRoundedValue;
intValue = (Int32)(Double.Parse(strValue));
}
//intValue = Math.Abs(intValue);
//finalValue = intValue .ToString();
file[i].Value = (Math.Abs(intValue)).ToString();
}
双dblRoundedValue;
Int32 intValue;
Int32 INTABS值;
字符串最终值;
for(int i=0;i
但是我不理解这两个Double.Parse。如果需要,请将它们放回
并将其转换为并行
ParallelOptions parallelOptions = new ParallelOptions();
parallelOptions.MaxDegreeOfParallelism = 16;
Parallel.For(0, values.Length, parallelOptions, i =>
{
strValue = values[j];
if (!Int32.TryParse(strValue, out intValue))
{
intValue = (Int32)(Double.Parse(strValue));
}
if (intValue < 0) intValue = -intValue;
file[i].Value = intValue.ToString();
});
ParallelOptions ParallelOptions=new ParallelOptions();
parallelOptions.MaxDegreeOfParallelism=16;
Parallel.For(0,values.Length,parallelOptions,i=>
{
strValue=值[j];
如果(!Int32.TryParse(strValue,out intValue))
{
intValue=(Int32)(Double.Parse(strValue));
}
如果(intValue<0)intValue=-intValue;
文件[i].Value=intValue.ToString();
});
我认为格伦奇会是最快的
string[] values = new string[] { "1", "2", "3", "1.5", "56.5", "8" };
string[] files = new string[values.Length];
HashSet<char> lt5 = new HashSet<char> {'0','1','2','3','4'};
bool haveDecimal;
bool haveDecimalConfirmed;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < values.Length; i++)
{
sb.Clear();
haveDecimal = false;
haveDecimalConfirmed = false;
foreach(char c in values[i])
{
if (haveDecimal)
{
if (lt5.Contains(c))
{
files[i] = sb.ToString();
}
else
{
files[i] = (Int32.Parse(sb.ToString()) + 1).ToString();
}
haveDecimalConfirmed = true;
break;
}
else if (c == '.')
{
haveDecimal = true;
continue;
}
if (c == '-') continue;
sb.Append(c);
}
if (!haveDecimalConfirmed) files[i] = sb.ToString();
}
string[]values=新字符串[]{“1”、“2”、“3”、“1.5”、“56.5”、“8”};
string[]files=新字符串[values.Length];
HashSet lt5=新的HashSet{'0','1','2','3','4'};
布尔哈维;
布尔哈维小数确认;
StringBuilder sb=新的StringBuilder();
for(int i=0;i
双数据值;
双dblRoundedValue;
Int32 intValue;
Int32 INTABS值;
字符串最终值;
for(int i=0;i
但是我不理解这两个Double.Parse。如果需要,请将它们放回
并将其转换为并行
ParallelOptions parallelOptions = new ParallelOptions();
parallelOptions.MaxDegreeOfParallelism = 16;
Parallel.For(0, values.Length, parallelOptions, i =>
{
strValue = values[j];
if (!Int32.TryParse(strValue, out intValue))
{
intValue = (Int32)(Double.Parse(strValue));
}
if (intValue < 0) intValue = -intValue;
file[i].Value = intValue.ToString();
});
ParallelOptions ParallelOptions=new ParallelOptions();
parallelOptions.MaxDegreeOfParallelism=16;
Parallel.For(0,values.Length,parallelOptions,i=>
{
strValue=值[j];
如果(!Int32.TryParse(strValue,out intValue))
{
intValue=(Int32)(Double.Parse(strValue));
}
如果(intValue<0)intValue=-intValue;
文件[i].Value=intValue.ToString();
});
我认为格伦奇会是最快的
string[] values = new string[] { "1", "2", "3", "1.5", "56.5", "8" };
string[] files = new string[values.Length];
HashSet<char> lt5 = new HashSet<char> {'0','1','2','3','4'};
bool haveDecimal;
bool haveDecimalConfirmed;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < values.Length; i++)
{
sb.Clear();
haveDecimal = false;
haveDecimalConfirmed = false;
foreach(char c in values[i])
{
if (haveDecimal)
{
if (lt5.Contains(c))
{
files[i] = sb.ToString();
}
else
{
files[i] = (Int32.Parse(sb.ToString()) + 1).ToString();
}
haveDecimalConfirmed = true;
break;
}
else if (c == '.')
{
haveDecimal = true;
continue;
}
if (c == '-') continue;
sb.Append(c);
}
if (!haveDecimalConfirmed) files[i] = sb.ToString();
}
string[]values=新字符串[]{“1”、“2”、“3”、“1.5”、“56.5”、“8”};
string[]files=新字符串[values.Length];
HashSet lt5=新的HashSet{'0','1','2','3','4'};
布尔哈维;
布尔哈维马尔科酒店