如何确保零始终在最后

pro*_*ook 0 .net c#

我需要确保double始终以0结尾

6 = 6.0  
5.9876577878 = 5.98765778780
Run Code Online (Sandbox Code Playgroud)

我尝试了格式,但没有带来预期的结果.

如何才能做到这一点?

Joe*_*oel 5

不确定String.Format方法是否允许您执行此操作.这是你可以做的事情:

public static class DoubleUtils {
    public static String EnsureEndsWithZero(this double value) {
        String str = value.ToString();
        if(!str.EndsWith("0")) {
            if(str.Contains(".")) {
                str += "0";
            } else {
                str += ".0";
            }
        }
        return str;
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

double val = 10.1;
Console.WriteLine(val.EnsureEndsWithZero());
Run Code Online (Sandbox Code Playgroud)