小数点变成整数

Make decimal point to whole numbers

我有 textBox1,它包含数字 10。我需要让数字 10 加倍带小数点。我当前的代码是:

variables.myNum = double.Parse(textBox1);

我已经在变量中声明了 myNum class:

public static double myNum;

使用该代码,我将数字 10 加倍。问题是我也需要有小数点所以我想要“10.”。如果我给 textBox1 写“10”。没用。

编辑:.ToString("0.00") 给出整数的小数点,但它也给出我不想要的那两个零。整数只需要一点。

编辑:首先我有:

10
10.5
10.58
10.589

我想要:(注意第一个十之后的点)

10.
10.5
10.58
10.589

使用 .ToString("0.00") 我可以得到:

10.00
10.50
10.58
10.58

但我不想要最后那些多余的零。只有不为零的点和数字。

static void Main(string[] args)
{
    Console.WriteLine(Test1());
    Console.WriteLine(Test2());
    Console.ReadLine();
}

static decimal Test1()
{
    return 10.999999999999999999999M;
}
static decimal Test2()
{
    return (decimal)10.999999999999999999999;
}

第一个returns10.999999999999999999999 但第二个returns11

如果输出中不包含一个 .,您可以只添加一个。

public static string ToGCodeNumber(double number, int digits)
{
    // rounding (optional)
    double number = Math.Round(number, digits);

    // result with or without '.'
    string intermediateResult = roundedNumber.ToString(CultureInfo.InvariantCulture);
    if(!intermediateResult.Contains(".")
        intermediateResult += ".";

    // final result with a guaranteed '.'  
    return intermediateResult;
}