String.Format 获取“2.4k”从“2400”开始

发布于 2024-10-31 22:36:12 字数 119 浏览 1 评论 0原文

有没有办法使用 String.Format 将数字转换为数字/字符表示形式。

例如

2400 -> 2.4k
2,600,000 -> 2.6m

Is there a way to use String.Format to convert numbers to number/character representations.

For example

2400 -> 2.4k
2,600,000 -> 2.6m

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

或十年 2024-11-07 22:36:12

我提供示例代码。请尝试按照你的方式去做

private static string ToEngineeringNotation(this double d)
{
    double exponent = Math.Log10(Math.Abs(d));
    if (Math.Abs(d) >= 1)
    {
        switch ((int)Math.Floor(exponent))
        {
            case 0: case 1: case 2:
                return d.ToString();
            case 3: case 4: case 5:
                return (d / 1e3).ToString() + "k";
            case 6: case 7: case 8:
                return (d / 1e6).ToString() + "M";
            case 9: case 10: case 11:
                return (d / 1e9).ToString() + "G";
            case 12: case 13: case 14:
                return (d / 1e12).ToString() + "T";
            case 15: case 16: case 17:
                return (d / 1e15).ToString() + "P";
            case 18: case 19: case 20:
                return (d / 1e18).ToString() + "E";
            case 21: case 22: case 23:
                return (d / 1e21).ToString() + "Z";
            default:
                return (d / 1e24).ToString() + "Y";
        }
    }
    else if (Math.Abs(d) > 0)
    {
        switch ((int)Math.Floor(exponent))
        {
            case -1: case -2: case -3:
                return (d * 1e3).ToString() + "m";
            case -4: case -5: case -6:
                return (d * 1e6).ToString() + "μ";
            case -7: case -8: case -9:
                return (d * 1e9).ToString() + "n";
            case -10: case -11: case -12:
                return (d * 1e12).ToString() + "p";
            case -13: case -14: case -15:
                return (d * 1e15).ToString() + "f";
            case -16: case -17: case -18:
                return (d * 1e18).ToString() + "a";
            case -19: case -20: case -21:
                return (d * 1e21).ToString() + "z";
            default:
                return (d * 1e24).ToString() + "y";
        }
    }
    else
    {
        return "0";
    }
}

I am providing a sample code. Please try to make it in your way

private static string ToEngineeringNotation(this double d)
{
    double exponent = Math.Log10(Math.Abs(d));
    if (Math.Abs(d) >= 1)
    {
        switch ((int)Math.Floor(exponent))
        {
            case 0: case 1: case 2:
                return d.ToString();
            case 3: case 4: case 5:
                return (d / 1e3).ToString() + "k";
            case 6: case 7: case 8:
                return (d / 1e6).ToString() + "M";
            case 9: case 10: case 11:
                return (d / 1e9).ToString() + "G";
            case 12: case 13: case 14:
                return (d / 1e12).ToString() + "T";
            case 15: case 16: case 17:
                return (d / 1e15).ToString() + "P";
            case 18: case 19: case 20:
                return (d / 1e18).ToString() + "E";
            case 21: case 22: case 23:
                return (d / 1e21).ToString() + "Z";
            default:
                return (d / 1e24).ToString() + "Y";
        }
    }
    else if (Math.Abs(d) > 0)
    {
        switch ((int)Math.Floor(exponent))
        {
            case -1: case -2: case -3:
                return (d * 1e3).ToString() + "m";
            case -4: case -5: case -6:
                return (d * 1e6).ToString() + "μ";
            case -7: case -8: case -9:
                return (d * 1e9).ToString() + "n";
            case -10: case -11: case -12:
                return (d * 1e12).ToString() + "p";
            case -13: case -14: case -15:
                return (d * 1e15).ToString() + "f";
            case -16: case -17: case -18:
                return (d * 1e18).ToString() + "a";
            case -19: case -20: case -21:
                return (d * 1e21).ToString() + "z";
            default:
                return (d * 1e24).ToString() + "y";
        }
    }
    else
    {
        return "0";
    }
}
吻泪 2024-11-07 22:36:12

不,我不相信有一个格式字符串可以为您做到这一点。

您可能会找到第三方库来完成此操作,并且我知道有内置的 Win32 例程可以将文件大小转换为类似的表示形式,但这很可能使用 1024 而不是 1000 K/M等的“基地”。 这个 Stack Overflow 答案 显示了一些 C# 代码,但我确信有一些东西在平台中也是如此......正如我所说,这是针对文件大小的,这可能是也可能不是您想要的。

No, I don't believe there's a format string that will do that for you.

You may well find third party libraries to do it, and I know there are built-in Win32 routines to convert a file size to a representation like that, but that may well use 1024 rather than 1000 for the "base" of the K/M/etc. This Stack Overflow answer shows some C# code for it, but I'm sure there's something in the platform too... and as I say, that's aimed at file sizes, which may or may not be what you want.

無心 2024-11-07 22:36:12

我为你写了这个。

string onem = intToSimple(1000000);
string onek = intToSimple(1000);
private string intToSimple(int number)
{
    if(val > 1000000000000)
        return (val / 1000000000000).ToString("0.00") + "tr";
    else if(val > 1000000000)
        return (val / 1000000000).ToString("0.00") + "b";
    else if(val > 1000000)
        return (val / 1000000).ToString("0.00") + "m";
    else if(val > 1000)
        return (val / 1000).ToString("0.00") + "k";
    else
        return value.ToString("0.00");
}

I wrote this for you.

string onem = intToSimple(1000000);
string onek = intToSimple(1000);
private string intToSimple(int number)
{
    if(val > 1000000000000)
        return (val / 1000000000000).ToString("0.00") + "tr";
    else if(val > 1000000000)
        return (val / 1000000000).ToString("0.00") + "b";
    else if(val > 1000000)
        return (val / 1000000).ToString("0.00") + "m";
    else if(val > 1000)
        return (val / 1000).ToString("0.00") + "k";
    else
        return value.ToString("0.00");
}
偏爱你一生 2024-11-07 22:36:12

您还可以尝试以下操作:

将您的数字转换为科学格式的字符串:2400 -> 2.4e+003; 2,6000,000 -> 2,6000,000 2.6e+006

然后用所需的 SI 前缀替换指数(例如 e+003 -> k、e+006 -> M、e-009 -> n)

为此目的,我创建了以下扩展:

using System;
using System.Globalization;

/// <summary>
/// Si prefixed string conversion class
/// </summary>
public static class SIPrefixedString
{
    /// <summary>
    /// converts the value into a string with SI prefix
    /// </summary>
    /// <param name="value">The value.</param>
    /// <returns>si prefixed string</returns>
    public static string ToSIPrefixedString(this double value)
    {
        string stringValue = value.ToString("#E+00", CultureInfo.InvariantCulture);
        string[] stringValueParts = stringValue.Split("E".ToCharArray());
        int mantissa = Convert.ToInt32(stringValueParts[0], CultureInfo.InvariantCulture);
        int exponent = Convert.ToInt32(stringValueParts[1], CultureInfo.InvariantCulture);
        while (exponent % 3 != 0)
        {
            mantissa *= 10;
            exponent -= 1;
        }

        string prefixedValue = mantissa.ToString(CultureInfo.InvariantCulture);
        switch (exponent)
        {
            case 24:
                prefixedValue += "Y";
                break;
            case 21:
                prefixedValue += "Z";
                break;
            case 18:
                prefixedValue += "E";
                break;
            case 15:
                prefixedValue += "P";
                break;
            case 12:
                prefixedValue += "T";
                break;
            case 9:
                prefixedValue += "G";
                break;
            case 6:
                prefixedValue += "M";
                break;
            case 3:
                prefixedValue += "k";
                break;
            case 0:
                break;
            case -3:
                prefixedValue += "m";
                break;
            case -6:
                prefixedValue += "u";
                break;
            case -9:
                prefixedValue += "n";
                break;
            case -12:
                prefixedValue += "p";
                break;
            case -15:
                prefixedValue += "f";
                break;
            case -18:
                prefixedValue += "a";
                break;
            case -21:
                prefixedValue += "z";
                break;
            case -24:
                prefixedValue += "y";
                break;
            default:
                prefixedValue = "invalid";
                break;
        }

        return prefixedValue;
    }

    /// <summary>
    /// returns the double value for the si prefixed string
    /// </summary>
    /// <param name="prefixedValue">The prefixed value.</param>
    /// <returns>double value</returns>
    public static double FromSIPrefixedString(this string prefixedValue)
    {
        string scientificNotationValue = prefixedValue;

        if (scientificNotationValue.Contains("E+") == false && scientificNotationValue.Contains("E-") == false)
        {
            scientificNotationValue = scientificNotationValue
                .Replace("Y", "E+24")
                .Replace("Z", "E+21")
                .Replace("E", "E+18")
                .Replace("P", "E+15")
                .Replace("T", "E+12")
                .Replace("G", "E+09")
                .Replace("M", "E+06")
                .Replace("k", "E+03")
                .Replace("m", "E-03")
                .Replace("u", "E-06")
                .Replace("n", "E-09")
                .Replace("p", "E-12")
                .Replace("f", "E-15")
                .Replace("a", "E-18")
                .Replace("z", "E-21")
                .Replace("y", "E-24");
        }

        return Convert.ToDouble(scientificNotationValue, CultureInfo.InvariantCulture);
    }

You can also try the following:

Convert your number into a string in scientific format: 2400 -> 2.4e+003; 2,6000,000 -> 2.6e+006

Then replace the exponent with the desired SI-prefix (e.g. e+003 -> k, e+006 -> M, e-009 -> n)

I've created the following extension for this purpose:

using System;
using System.Globalization;

/// <summary>
/// Si prefixed string conversion class
/// </summary>
public static class SIPrefixedString
{
    /// <summary>
    /// converts the value into a string with SI prefix
    /// </summary>
    /// <param name="value">The value.</param>
    /// <returns>si prefixed string</returns>
    public static string ToSIPrefixedString(this double value)
    {
        string stringValue = value.ToString("#E+00", CultureInfo.InvariantCulture);
        string[] stringValueParts = stringValue.Split("E".ToCharArray());
        int mantissa = Convert.ToInt32(stringValueParts[0], CultureInfo.InvariantCulture);
        int exponent = Convert.ToInt32(stringValueParts[1], CultureInfo.InvariantCulture);
        while (exponent % 3 != 0)
        {
            mantissa *= 10;
            exponent -= 1;
        }

        string prefixedValue = mantissa.ToString(CultureInfo.InvariantCulture);
        switch (exponent)
        {
            case 24:
                prefixedValue += "Y";
                break;
            case 21:
                prefixedValue += "Z";
                break;
            case 18:
                prefixedValue += "E";
                break;
            case 15:
                prefixedValue += "P";
                break;
            case 12:
                prefixedValue += "T";
                break;
            case 9:
                prefixedValue += "G";
                break;
            case 6:
                prefixedValue += "M";
                break;
            case 3:
                prefixedValue += "k";
                break;
            case 0:
                break;
            case -3:
                prefixedValue += "m";
                break;
            case -6:
                prefixedValue += "u";
                break;
            case -9:
                prefixedValue += "n";
                break;
            case -12:
                prefixedValue += "p";
                break;
            case -15:
                prefixedValue += "f";
                break;
            case -18:
                prefixedValue += "a";
                break;
            case -21:
                prefixedValue += "z";
                break;
            case -24:
                prefixedValue += "y";
                break;
            default:
                prefixedValue = "invalid";
                break;
        }

        return prefixedValue;
    }

    /// <summary>
    /// returns the double value for the si prefixed string
    /// </summary>
    /// <param name="prefixedValue">The prefixed value.</param>
    /// <returns>double value</returns>
    public static double FromSIPrefixedString(this string prefixedValue)
    {
        string scientificNotationValue = prefixedValue;

        if (scientificNotationValue.Contains("E+") == false && scientificNotationValue.Contains("E-") == false)
        {
            scientificNotationValue = scientificNotationValue
                .Replace("Y", "E+24")
                .Replace("Z", "E+21")
                .Replace("E", "E+18")
                .Replace("P", "E+15")
                .Replace("T", "E+12")
                .Replace("G", "E+09")
                .Replace("M", "E+06")
                .Replace("k", "E+03")
                .Replace("m", "E-03")
                .Replace("u", "E-06")
                .Replace("n", "E-09")
                .Replace("p", "E-12")
                .Replace("f", "E-15")
                .Replace("a", "E-18")
                .Replace("z", "E-21")
                .Replace("y", "E-24");
        }

        return Convert.ToDouble(scientificNotationValue, CultureInfo.InvariantCulture);
    }
蓝色星空 2024-11-07 22:36:12

您将无法使用 String.Format 执行此操作,但您可以尝试以下操作:

int i = 2400;
string j = i/1000.0 + "k";
Console.WriteLine(j);

You won't be able to do this using String.Format but you can try this:

int i = 2400;
string j = i/1000.0 + "k";
Console.WriteLine(j);
从来不烧饼 2024-11-07 22:36:12

我认为你的问题很普遍,无论如何我为你发布了一些案例的简单答案:

private string Cnv(int num)
{
    double DIV=1000f;

    double f = num;
    if (f<DIV) return num.ToString();
    f = num / DIV;
    if (f < DIV) return f.ToString("0.0k");
    f = num / DIV;
    if (f < DIV) return f.ToString("0.0m");
    return (f / DIV).ToString("0.0g");
}

I think your question is general, anyway I post you a simple answer for some case:

private string Cnv(int num)
{
    double DIV=1000f;

    double f = num;
    if (f<DIV) return num.ToString();
    f = num / DIV;
    if (f < DIV) return f.ToString("0.0k");
    f = num / DIV;
    if (f < DIV) return f.ToString("0.0m");
    return (f / DIV).ToString("0.0g");
}
请恋爱 2024-11-07 22:36:12

这是扩展形式的正确形式。

public static string ToSimpleK(this int val)
{
    if (val > 1000000000)
        return ((decimal)val / 1000000000).ToString("0.00") + "b";
    else if (val > 1000000)
        return ((decimal)val / 1000000).ToString("0.00") + "m";
    else if (val > 1000)
        return ((decimal)val / 1000).ToString("0.00") + "k";
    else
        return val.ToString();
}

this is correct one in extention form.

public static string ToSimpleK(this int val)
{
    if (val > 1000000000)
        return ((decimal)val / 1000000000).ToString("0.00") + "b";
    else if (val > 1000000)
        return ((decimal)val / 1000000).ToString("0.00") + "m";
    else if (val > 1000)
        return ((decimal)val / 1000).ToString("0.00") + "k";
    else
        return val.ToString();
}
女皇必胜 2024-11-07 22:36:12

功能:

static string doubleTFormattedString(double d) {
    if (d >= 1e12) {
        return string.Format("{0}T", (d / 1e12).ToString("0.00"));
    }

    if (d >= 1e9) {
        return string.Format("{0}B", (d / 1e9).ToString("0.00"));
    }

    if (d >= 1e6) {
        return string.Format("{0}M", (d / 1e6).ToString("0.00"));
    }

    if (d >= 1e3) {
        return string.Format("{0}K", (d / 1e3).ToString("0.00"));
    }

    return d.ToString("0.00");
}

The function:

static string doubleTFormattedString(double d) {
    if (d >= 1e12) {
        return string.Format("{0}T", (d / 1e12).ToString("0.00"));
    }

    if (d >= 1e9) {
        return string.Format("{0}B", (d / 1e9).ToString("0.00"));
    }

    if (d >= 1e6) {
        return string.Format("{0}M", (d / 1e6).ToString("0.00"));
    }

    if (d >= 1e3) {
        return string.Format("{0}K", (d / 1e3).ToString("0.00"));
    }

    return d.ToString("0.00");
}
苏大泽ㄣ 2024-11-07 22:36:12

这是一个单行代码,类似于:

string Format(long value) =>
    "kMBTP"
        .Scan(
            (symbol: "", magnitude: 1L),
            (a, x) => (symbol: x.ToString(), magnitude: a.magnitude * 1000))
        .Select(x => (symbol: x.symbol, magnitude: x.magnitude / 10))
        .Reverse()
        .Aggregate(
            (symbol: "", value: value),
            (a, x) => (a.symbol == "" && a.value % x.magnitude == 0L) ? (symbol: x.symbol, value: a.value / x.magnitude) : a,
            x => $"{(x.value / 10.0):0.0}{x.symbol}");

Console.WriteLine(Format(2400));
Console.WriteLine(Format(2600000));

产生:

2.4k
2.6M

注意:它使用 System.Interactive 作为 Scan 操作符。

Here's a one-liner, sort of:

string Format(long value) =>
    "kMBTP"
        .Scan(
            (symbol: "", magnitude: 1L),
            (a, x) => (symbol: x.ToString(), magnitude: a.magnitude * 1000))
        .Select(x => (symbol: x.symbol, magnitude: x.magnitude / 10))
        .Reverse()
        .Aggregate(
            (symbol: "", value: value),
            (a, x) => (a.symbol == "" && a.value % x.magnitude == 0L) ? (symbol: x.symbol, value: a.value / x.magnitude) : a,
            x => 
quot;{(x.value / 10.0):0.0}{x.symbol}");

Console.WriteLine(Format(2400));
Console.WriteLine(Format(2600000));

That produces:

2.4k
2.6M

NB: It uses System.Interactive for the Scan operator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文