C#.NET动态尾随零?
我正在写一个库来格式化数字。存在需要指定尾随零数的问题。这方面的一个例子是财务数据,其中某些价格显示的小数位数超过2位,但只有前两位小数可以是尾随的零 示例(给出2个尾随零和精度4): 0.10000=>0.10 0.11100=>0.111 0.119=>0.119 0.11119=>0.1112 我还想将其与System.Globalization提供的“p”和“C”格式结合使用。示例: 0.587“C”=>0.587美元 0.20“C”=>0.20美元 0.20001“C”=>0.20美元 0.48745“C”=>0.4875美元 或 0.587“p”=>58.70% 0.20“C”=>20.00% 0.20001“C”=>20.00% 0.48745“C”=>48.745% 我希望能够向库提供可变数量的尾随零 我想利用内置的功能,以便利用全球化。由于.ToString和String.Format允许指定要显示的小数位数,我想我可以使用一些奇特的对数数学来确定是否应该显示小数位数。我想我会看看是否有更好的解决方案。您可以使用:C#.NET动态尾随零?,c#,.net,C#,.net,我正在写一个库来格式化数字。存在需要指定尾随零数的问题。这方面的一个例子是财务数据,其中某些价格显示的小数位数超过2位,但只有前两位小数可以是尾随的零 示例(给出2个尾随零和精度4): 0.10000=>0.10 0.11100=>0.111 0.119=>0.119 0.11119=>0.1112 我还想将其与System.Globalization提供的“p”和“C”格式结合使用。示例: 0.587“C”=>0.587美元 0.20“C”=>0.20美元 0.20001“C”=>0.20美元
number.ToString("0.00##")
及
比如说
var inputs = new[] { "0.1000", "0.11100", "0.119", "0.11119" };
var numbers = inputs.Select(decimal.Parse);
var output = numbers.Select(x => x.ToString("0.00##"));
Console.WriteLine(String.Join(", ", output));
输出
0.10, 0.111, 0.119, 0.1112
就百分比和货币而言,我不认为有任何聪明的方法可以做到这一点,但可以将以下内容与上述内容结合起来: 通货 百分比 您可以使用:
number.ToString("0.00##")
及
比如说
var inputs = new[] { "0.1000", "0.11100", "0.119", "0.11119" };
var numbers = inputs.Select(decimal.Parse);
var output = numbers.Select(x => x.ToString("0.00##"));
Console.WriteLine(String.Join(", ", output));
输出
0.10, 0.111, 0.119, 0.1112
就百分比和货币而言,我不认为有任何聪明的方法可以做到这一点,但可以将以下内容与上述内容结合起来: 通货 百分比
我最后写了一个简短的算法,可以给出适当的精度。这可以与“PX”、“NX”或“CX”结合使用,以用于全球化和内置的.ToString格式。它已经用各种数字进行了单元测试,所以它是健壮的
/// <summary>Calculate the correct precision based on trailing zeroes and target precision
/// </summary>
/// <param name="number">the number to check (multiply by 100 if it is being formatted as a percent)</param>
/// <param name="trailingZeroes">the number of trailing zeroes</param>
/// <param name="precision">the number of decimal places to show</param>
/// <returns>
/// Argument Exception: trailing zero and precision cannot be less than one or greater than 99
/// Argument Exception: trailing zeros cannot be larger than the precision
/// </returns>
private int CalculatePrecision(double number, int trailingZeroes, int precision)
{
if (trailingZeroes < 0 || trailingZeroes > 99 || precision < 0 || precision > 99)
{
throw new ArgumentException("Trailing Zeroes and precision must be between 0 and 99.");
}
// make sure trailng zeroes is not larger than precision
trailingZeroes = Math.Min(trailingZeroes, precision);
// a temporary value for locating decimal places
double tempValue;
// just the decimal places
double allDecimalPlaces = number - Math.Floor(number);
// the individual decimal place at a given power of 10
double singleDecimalPlaceValue;
// search for the zeroes in the decimal places
for (int i = precision; i > trailingZeroes; i--)
{
// get just the decimal place that needs to be checked
tempValue = allDecimalPlaces * Math.Pow(10, i - 1);
singleDecimalPlaceValue = Math.Round((tempValue - Math.Floor(tempValue)) * 10);
// check for a 0 decimal or a 10 value (for floating point math)
if (singleDecimalPlaceValue != 0 && singleDecimalPlaceValue != 10)
{
return i;
}
}
// if all zeroes are found, return trailing zeroes
return trailingZeroes;
}
///根据尾随零和目标精度计算正确的精度
///
///要检查的数字(如果格式化为百分比,则乘以100)
///尾随零的数目
///要显示的小数位数
///
///参数异常:尾随零和精度不能小于1或大于99
///参数异常:尾随零不能大于精度
///
专用整数计算精度(双倍数字、整数跟踪零、整数精度)
{
if(跟踪零点<0 | |跟踪零点>99 | | |精度<0 | |精度>99)
{
抛出新ArgumentException(“尾随的零和精度必须介于0和99之间”);
}
//确保跟踪零点不大于精度
trailingZeroes=Math.Min(trailingZeroes,精度);
//用于定位小数位的临时值
双温值;
//只有小数点
double allDecimalPlaces=数字-数学楼层(数字);
//给定的10次方的小数位数
双单十进制值;
//搜索小数点后的零
对于(int i=精度;i>跟踪零;i--)
{
//只获取需要检查的小数位
tempValue=allDecimalPlaces*Math.Pow(10,i-1);
singleDecimalPlaceValue=Math.Round((tempValue-Math.Floor(tempValue))*10);
//检查十进制0或10值(对于浮点数学)
if(singleDecimalPlaceValue!=0&&singleDecimalPlaceValue!=10)
{
返回i;
}
}
//如果找到所有零,则返回尾随的零
返回跟踪零;
}
我最后写了一个简短的算法,可以给出适当的精度。这可以与“PX”、“NX”或“CX”结合使用,以用于全球化和内置的.ToString格式。它已经用各种数字进行了单元测试,所以它是健壮的
/// <summary>Calculate the correct precision based on trailing zeroes and target precision
/// </summary>
/// <param name="number">the number to check (multiply by 100 if it is being formatted as a percent)</param>
/// <param name="trailingZeroes">the number of trailing zeroes</param>
/// <param name="precision">the number of decimal places to show</param>
/// <returns>
/// Argument Exception: trailing zero and precision cannot be less than one or greater than 99
/// Argument Exception: trailing zeros cannot be larger than the precision
/// </returns>
private int CalculatePrecision(double number, int trailingZeroes, int precision)
{
if (trailingZeroes < 0 || trailingZeroes > 99 || precision < 0 || precision > 99)
{
throw new ArgumentException("Trailing Zeroes and precision must be between 0 and 99.");
}
// make sure trailng zeroes is not larger than precision
trailingZeroes = Math.Min(trailingZeroes, precision);
// a temporary value for locating decimal places
double tempValue;
// just the decimal places
double allDecimalPlaces = number - Math.Floor(number);
// the individual decimal place at a given power of 10
double singleDecimalPlaceValue;
// search for the zeroes in the decimal places
for (int i = precision; i > trailingZeroes; i--)
{
// get just the decimal place that needs to be checked
tempValue = allDecimalPlaces * Math.Pow(10, i - 1);
singleDecimalPlaceValue = Math.Round((tempValue - Math.Floor(tempValue)) * 10);
// check for a 0 decimal or a 10 value (for floating point math)
if (singleDecimalPlaceValue != 0 && singleDecimalPlaceValue != 10)
{
return i;
}
}
// if all zeroes are found, return trailing zeroes
return trailingZeroes;
}
///根据尾随零和目标精度计算正确的精度
///
///要检查的数字(如果格式化为百分比,则乘以100)
///尾随零的数目
///要显示的小数位数
///
///参数异常:尾随零和精度不能小于1或大于99
///参数异常:尾随零不能大于精度
///
专用整数计算精度(双倍数字、整数跟踪零、整数精度)
{
if(跟踪零点<0 | |跟踪零点>99 | | |精度<0 | |精度>99)
{
抛出新ArgumentException(“尾随的零和精度必须介于0和99之间”);
}
//确保跟踪零点不大于精度
trailingZeroes=Math.Min(trailingZeroes,精度);
//用于定位小数位的临时值
双温值;
//只有小数点
double allDecimalPlaces=数字-数学楼层(数字);
//给定的10次方的小数位数
双单十进制值;
//搜索小数点后的零
对于(int i=精度;i>跟踪零;i--)
{
//只获取需要检查的小数位
tempValue=allDecimalPlaces*Math.Pow(10,i-1);
singleDecimalPlaceValue=Math.Round((tempValue-Math.Floor(tempValue))*10);
//检查十进制0或10值(对于浮点数学)
if(singleDecimalPlaceValue!=0&&singleDecimalPlaceValue!=10)
{
返回i;
}
}
//如果找到所有零,则返回尾随的零
返回跟踪零;
}
如果您使用“0。