C# 截断第一个正整数以外的小数

C# 截断第一个正整数以外的小数,c#,decimal,truncate,C#,Decimal,Truncate,C#-截断第一个正整数以外的小数 一个数字是0.009012 结果应为0.009 或者是1.1234和1.1 或2.099~2.09 等等 以快速、最佳的方式尝试以下方法: private static string RoundSpecial(double x) { string s = x.ToString(); int dotPos = s.IndexOf('.'); if (dotPos != -1) { int notZeroPos = s

C#-截断第一个正整数以外的小数

一个数字是
0.009012

结果应为
0.009

或者是
1.1234
1.1

2.099
~
2.09
等等

以快速、最佳的方式尝试以下方法:

private static string RoundSpecial(double x)
{
    string s = x.ToString();
    int dotPos = s.IndexOf('.');
    if (dotPos != -1)
    {
        int notZeroPos = s.IndexOfAny(new[] { '1', '2', '3', '4', '5', '6', '7', '8', '9' }, dotPos + 1);
        return notZeroPos == -1 ? s : s.Substring(0, notZeroPos + 1);
    }
    return s;
}
我不确定它是否是最快和最佳的方法,但它能满足你的需要

第二种方法是使用
Log10
%

private static double RoundSpecial(double x)
{
    int pos = -(int)Math.Log10(x - (int)x);
    return Math.Round(x - (x % Math.Pow(0.1, pos + 1)), pos + 1);
}
尝试以下方法:

private static string RoundSpecial(double x)
{
    string s = x.ToString();
    int dotPos = s.IndexOf('.');
    if (dotPos != -1)
    {
        int notZeroPos = s.IndexOfAny(new[] { '1', '2', '3', '4', '5', '6', '7', '8', '9' }, dotPos + 1);
        return notZeroPos == -1 ? s : s.Substring(0, notZeroPos + 1);
    }
    return s;
}
我不确定它是否是最快和最佳的方法,但它能满足你的需要

第二种方法是使用
Log10
%

private static double RoundSpecial(double x)
{
    int pos = -(int)Math.Log10(x - (int)x);
    return Math.Round(x - (x % Math.Pow(0.1, pos + 1)), pos + 1);
}

您也可以尝试正则表达式:

decimal d = 2.0012m;            

Regex pattern = new Regex(@"^(?<num>(0*[1-9]))\d*$");
Match match = pattern.Match(d.ToString().Split('.')[1]);
string afterDecimal = match.Groups["num"].Value; //afterDecimal = 001
十进制d=2.0012m;
正则表达式模式=新正则表达式(@“^(?(0*[1-9]))\d*$”;
Match=pattern.Match(d.ToString().Split('.')[1]);
字符串afterDecimal=match.Groups[“num”].Value//小数点后=001

您也可以尝试正则表达式:

decimal d = 2.0012m;            

Regex pattern = new Regex(@"^(?<num>(0*[1-9]))\d*$");
Match match = pattern.Match(d.ToString().Split('.')[1]);
string afterDecimal = match.Groups["num"].Value; //afterDecimal = 001
十进制d=2.0012m;
正则表达式模式=新正则表达式(@“^(?(0*[1-9]))\d*$”;
Match=pattern.Match(d.ToString().Split('.')[1]);
字符串afterDecimal=match.Groups[“num”].Value//小数点后=001

这里有一个使用数学而不是字符串逻辑的解决方案:

double nr = 0.009012;
var partAfterDecimal = nr - (int) nr;
var partBeforeDecimal = (int) nr;

int count = 1;
partAfterDecimal*=10;
int number = (int)(partAfterDecimal);
while(number == 0)
{
  partAfterDecimal *= 10;
  number = (int)(partAfterDecimal);
  count++;
}

double dNumber = number;
while(count > 0){
 dNumber /= 10.0;
 count--;
}

double result = (double)partBeforeDecimal + dNumber;
result.Dump();

下面是一个使用数学而不是字符串逻辑的解决方案:

double nr = 0.009012;
var partAfterDecimal = nr - (int) nr;
var partBeforeDecimal = (int) nr;

int count = 1;
partAfterDecimal*=10;
int number = (int)(partAfterDecimal);
while(number == 0)
{
  partAfterDecimal *= 10;
  number = (int)(partAfterDecimal);
  count++;
}

double dNumber = number;
while(count > 0){
 dNumber /= 10.0;
 count--;
}

double result = (double)partBeforeDecimal + dNumber;
result.Dump();

从数学角度考虑,使用以10为底的对数

此函数只接受第一个密码

public double RoundOnFirstDecimal(double number)
{
    int shift = -1 * ((int)Math.Floor(Math.Log10(number)));
    return Math.Floor(number * Math.Pow(10, shift)) / Math.Pow(10, shift);
}
但您希望它是这样的:(将只针对小数部分而不是整数进行移位)


这将比任何正则表达式或循环快得多

从数学角度考虑,使用以10为底的对数

此函数只接受第一个密码

public double RoundOnFirstDecimal(double number)
{
    int shift = -1 * ((int)Math.Floor(Math.Log10(number)));
    return Math.Floor(number * Math.Pow(10, shift)) / Math.Pow(10, shift);
}
但您希望它是这样的:(将只针对小数部分而不是整数进行移位)


如果您不想使用
Math
库,那么这将比任何正则表达式或循环快得多:

    static double truncateMyWay(double x)
    {
        double afterDecimalPoint = x - (int)x;
        if (afterDecimalPoint == 0.0) return x;
        else
        {
            int i = 0;
            int count10 = 1;
            do 
            {
                afterDecimalPoint *= 10;
                count10 *= 10;
                i++;
            } while ((int) afterDecimalPoint == 0);

            return ((int)(x * count10))/((double)count10);
        }
    }

快乐的编码!;-)

如果您不想使用
Math
库:

    static double truncateMyWay(double x)
    {
        double afterDecimalPoint = x - (int)x;
        if (afterDecimalPoint == 0.0) return x;
        else
        {
            int i = 0;
            int count10 = 1;
            do 
            {
                afterDecimalPoint *= 10;
                count10 *= 10;
                i++;
            } while ((int) afterDecimalPoint == 0);

            return ((int)(x * count10))/((double)count10);
        }
    }

快乐的编码!;-)

是否要显示这些值?或者将它们用于计算?不是这样的,我认为最短解不是通过Carray实现的。你希望它快速,使用数学将比字符串解析更快。你要显示这些值吗?或者将其用于计算?事实并非如此,我认为最短解决方案不是通过Carray实现的。如果您希望它速度快,使用数学将比字符串解析快。使用
double
解析是一种很好的方法,可以获得一个位数与您想要的不同的数字。@Rawling谢谢您。已删除解析。还有其他问题吗?没有!它与OP的示例相匹配,即使OP有一种奇怪的舍入方式:)以
double
进行解析是一种很好的方法,可以获得一个位数与您想要的位数不同的数字。@Rawling谢谢您。已删除解析。还有其他问题吗?没有!它与OP的示例相匹配,即使OP有一种奇怪的舍入方式:)