简体   繁体   English

双C#的字符串格式

[英]String format from double C#

I have double for example 1.890. 我有两倍,例如1.890。 How I can format like this: 我该如何格式化:

  • if it is 100 must be 18.90 如果是100必须是18.90
  • if it is 10000 must be 0.1890 如果是10000必须是0.1890
  • if it is 100000 must be 0.01890 如果是100000必须是0.01890
  • if it is 10 must be 189.0 如果是10必须是189.0
  • if it is 1 must be 1890 如果是1必须是1890

I tried this: 我尝试了这个:

switch (price)
        {
            case (int)scales.zero:
                format = "{0:0}";
                break;
            case (int)scales.one:
                format = "{0:0}";
                break;
            case (int)scales.two:
                format = "{0:000}";
                break;
            case (int)scales.three:
                format = "{0:0000}";
                break;
            case (int)scales.four:
                format = "{0:00000}";
                break;
            case (int)scales.five:
                format = "{0:00000}";
                break;
            default:
                format = "{0:0.00}";
                break;
        }

        if (!string.IsNullOrWhiteSpace(format))
            res = string.Format(format, value);
        return res;

Your default clause includes a dot: {0:0.00}. 您的默认子句包含一个点:{0:0.00}。 Do it like that in the other cases: {0:0.00000} to have 5 zeroes. 在其他情况下,请这样做:{0:0.00000}具有5个零。 And divide the number by 100, 1000, etc. before formatting. 并在格式化前将数字除以100、1000等。

First of all, your results are little bit strange, honestly. 首先,说实话,您的结果有点奇怪。 For example; 例如; other than 1 case, all results have decimal separator but for 1 case, it doesn't have any thousand separator. 1情况外,所有结果都带有小数点分隔符,但对于1情况,它没有任何千位分隔符。 It is okey to solve this but seems strange to me. 解决这个问题还不错 ,但对我来说似乎很奇怪。

Second, I think your double is 1890 rather than 1.890 because when you use your enum values, looks like they divide your double value with their values with powered 10 . 其次,我认为您的double是1890而不是1.890因为当您使用枚举值时,看起来它们将double型值除以它们的值与幂10

If so, let's define your enum value first. 如果是这样,让我们​​先定义您的枚举值。

enum scales
{
    zero = 1,
    one = 10,
    two = 100,
    three = 1000,
    four = 10000,
    five = 100000
}

And second, create a culture that has . 其次,创造一种具有的文化. as a decimal separator and has empty string as a thousand separator. 作为小数点分隔符,并将空字符串作为千位分隔符。 For this, let's Clone a InvariantCulture and set it's NumberGroupSeparator to empty string. 为此,让我们Clone一个InvariantCulture并将其NumberGroupSeparator设置为空字符串。

var culture = (CultureInfo)CultureInfo.InvariantCulture.Clone();
culture.NumberFormat.NumberGroupSeparator = string.Empty;

Then we can use that culture to format our results using "N" format specifier and proper precision, after we divide our double value with the enum values that mathes. 然后,在将double值除以数学枚举值之后,可以使用该区域性使用"N"格式说明符适当的精度来格式化结果。

double d = 1890;
int price = 1;
string result = "";
switch (price)
{
    case (int)scales.zero:
         d = d / (int)scales.zero;
         result = d.ToString(culture);
         break;
    case (int)scales.one:
         d = d / (int)scales.one;
         result = d.ToString("N1", culture);
         break;
    case (int)scales.two:
         d = d / (int)scales.two;
         result = d.ToString("N2", culture);
         break;
    case (int)scales.three:
         d = d / (int)scales.three;
         result = d.ToString("N3", culture);
         break;
    case (int)scales.four:
         d = d / (int)scales.four;
         result = d.ToString("N4", culture);
         break;
    case (int)scales.five:
         d = d / (int)scales.five;
         result = d.ToString("N5", culture);
         break;
    default:
         break;
}

Console.WriteLine(result); 

You can change the value of price to 10 , 100 , 1000 , 10000 , 100000 and this will be generate exactly what results do you want. 你可以的值更改price ,以10100100010000100000这将是产生结果,你想要什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM