各位:
字符串中全部是数字,如:478632.32,1677732424.48
现在需求是这些字符串代表元,如何将上述字符串转化为万元单位的数字字串?四舍五入即可。
即:47.8万元 167773.2万元有比较简便的方法么?迫求!
字符串中全部是数字,如:478632.32,1677732424.48
现在需求是这些字符串代表元,如何将上述字符串转化为万元单位的数字字串?四舍五入即可。
即:47.8万元 167773.2万元有比较简便的方法么?迫求!
1.Round相较于Tostiring(“N1”),有何优越性?除了精度之外还有其他否?
2.对于我的需求,Round能直接实现么?还是也需要除以10000.能否给个例子。
拜谢!
{
class Program
{
static void Main(string[] args)
{
string str1 = "1677732424.48";//一般长的
string str2 = "1677211251251616735554.48";//二般长的
string str3 = "1677732251251516624.4825125125125";//很长的
//更长的 Console.WriteLine(Test(str1));
Console.WriteLine(Test(str2));
Console.WriteLine(Test(str3)); Console.Read();
}
static string Test(string input)
{
Regex regex = new Regex(@"^(?<wy>\d+)(?<qy>\d{4})+(|(.\d+))$", RegexOptions.IgnoreCase);
Match m = regex.Match(input);
if (m.Success)
{
int qy = (int.Parse(m.Groups["qy"].Value) + 500) / 1000;
return string.Format("{0}.{1}", m.Groups["wy"].Value, qy);
}
else
throw new Exception("不是有效的字符串");
}
}
}
这里为什么有个加500?菜鸟不懂!int qy = (int.Parse(m.Groups["qy"].Value) + 500) / 1000;
Decimal.Round活Math.Round,查一下MSDN,干活不会没这个吧
对于四舍五入微软有一些特别的解释