使用字符串格式显示最多2位小数或简单整数

问题描述 投票:0回答:18

我有一个要显示的价格字段,有时可以是 100 或 100.99 或 100.9,我想要的是仅当输入该价格的小数时才显示 2 位小数的价格,例如如果它是 100,那么它应该仅显示 100 而不是 100.00,如果价格为 100.2,则应显示 100.20,类似地,100.22 应该相同。 我用谷歌搜索并找到了一些例子,但它们与我想要的并不完全匹配:

// just two decimal places
String.Format("{0:0.00}", 123.4567);      // "123.46"
String.Format("{0:0.00}", 123.4);         // "123.40"
String.Format("{0:0.00}", 123.0);         // "123.00"
c# regex string-formatting number-formatting
18个回答
698
投票

很抱歉重新激活这个问题,但我在这里没有找到正确的答案。

在格式化数字时,您可以使用

0
作为必填位置,使用
#
作为可选位置。

所以:

// just two decimal places
String.Format("{0:0.##}", 123.4567);      // "123.46"
String.Format("{0:0.##}", 123.4);         // "123.4"
String.Format("{0:0.##}", 123.0);         // "123"

您还可以将

0
#
结合使用。

String.Format("{0:0.0#}", 123.4567)       // "123.46"
String.Format("{0:0.0#}", 123.4)          // "123.4"
String.Format("{0:0.0#}", 123.0)          // "123.0"

始终使用此格式化方法

CurrentCulture
。对于某些文化,
.
将更改为
,

原问题答案:

最简单的解决方案来自@Andrew(here)。所以我个人会使用这样的东西:

var number = 123.46;
number.ToString(number % 1 == 0 ? "0" : "0.00");

178
投票

一种不优雅的方式是:

var my = DoFormat(123.0);

DoFormat
类似于:

public static string DoFormat( double myNumber )
{
    var s = string.Format("{0:0.00}", myNumber);

    if ( s.EndsWith("00") )
    {
        return ((int)myNumber).ToString();
    }
    else
    {
        return s;
    }
}

不优雅,但在某些项目的类似情况下为我工作。


94
投票

这是常见的格式化浮点数用例。

不幸的是,所有内置的单字母格式字符串(例如 F、G、N)都无法直接实现这一点。
例如,

num.ToString("F2")
将始终显示 2 位小数,如
123.40

你必须使用

0.##
模式,即使它看起来有点冗长。

完整的代码示例:

double a = 123.4567;
double b = 123.40;
double c = 123.00;

string sa = a.ToString("0.##"); // 123.46
string sb = b.ToString("0.##"); // 123.4
string sc = c.ToString("0.##"); // 123

67
投票

老问题,但我想添加我认为最简单的选项。

没有千位分隔符:

value.ToString(value % 1 == 0 ? "F0" : "F2")

带有千位分隔符:

value.ToString(value % 1 == 0 ? "N0" : "N2")

相同,但使用 String.Format:

String.Format(value % 1 == 0 ? "{0:F0}" : "{0:F2}", value) // Without thousands separators
String.Format(value % 1 == 0 ? "{0:N0}" : "{0:N2}", value) // With thousands separators

如果你在很多地方需要它,我会在扩展方法中使用这个逻辑

public static string ToCoolString(this decimal value)
{
    return value.ToString(value % 1 == 0 ? "N0" : "N2"); // Or F0/F2 ;)
}

31
投票

尝试

double myPrice = 123.0;

String.Format(((Math.Round(myPrice) == myPrice) ? "{0:0}" : "{0:0.00}"), myPrice);

15
投票

如果您的程序需要快速运行,请调用 value.ToString(formatString) ,相对于 $"{value:formatString}" 和 string.Format(formatString, value),字符串格式化性能提高约 35%。

数据

代码

using System;
using System.Diagnostics;

public static class StringFormattingPerformance
{
   public static void Main()
   {
      Console.WriteLine("C# String Formatting Performance");
      Console.WriteLine("Milliseconds Per 1 Million Iterations - Best Of 5");
      long stringInterpolationBestOf5 = Measure1MillionIterationsBestOf5(
          (double randomDouble) =>
          {
             return $"{randomDouble:0.##}";
          });
      long stringDotFormatBestOf5 = Measure1MillionIterationsBestOf5(
          (double randomDouble) =>
          {
             return string.Format("{0:0.##}", randomDouble);
          });
      long valueDotToStringBestOf5 = Measure1MillionIterationsBestOf5(
          (double randomDouble) =>
          {
             return randomDouble.ToString("0.##");
          });
      Console.WriteLine(
$@"            $""{{value:formatString}}"": {stringInterpolationBestOf5} ms
 string.Format(formatString, value): {stringDotFormatBestOf5} ms
       value.ToString(formatString): {valueDotToStringBestOf5} ms");
   }

   private static long Measure1MillionIterationsBestOf5(
       Func<double, string> formatDoubleUpToTwoDecimalPlaces)
   {
      long elapsedMillisecondsBestOf5 = long.MaxValue;
      for (int perfRunIndex = 0; perfRunIndex < 5; ++perfRunIndex)
      {
         var random = new Random();
         var stopwatch = Stopwatch.StartNew();
         for (int i = 0; i < 1000000; ++i)
         {
            double randomDouble = random.NextDouble();
            formatDoubleUpToTwoDecimalPlaces(randomDouble);
         }
         stopwatch.Stop();
         elapsedMillisecondsBestOf5 = Math.Min(
            elapsedMillisecondsBestOf5, stopwatch.ElapsedMilliseconds);
      }
      return elapsedMillisecondsBestOf5;
   }
}

代码输出

C# String Formatting Performance
Milliseconds Per 1 Million Iterations - Best Of 5
            $"{value:formatString}": 419 ms
 string.Format(formatString, value): 419 ms
       value.ToString(formatString): 264 ms

参考文献

自定义数字格式字符串 [learn.microsoft.com]

Qt 图表 BarChart 示例 [doc.qt.io]


9
投票

我不知道如何在格式说明符中添加条件,但您可以编写自己的格式化程序:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
               // all of these don't work
            Console.WriteLine("{0:C}", 10);
            Console.WriteLine("{0:00.0}", 10);
            Console.WriteLine("{0:0}", 10);
            Console.WriteLine("{0:0.00}", 10);
            Console.WriteLine("{0:0}", 10.0);
            Console.WriteLine("{0:0}", 10.1);
            Console.WriteLine("{0:0.00}", 10.1);

          // works
            Console.WriteLine(String.Format(new MyFormatter(),"{0:custom}", 9));
            Console.WriteLine(String.Format(new MyFormatter(),"{0:custom}", 9.1));
            Console.ReadKey();
        }
    }

    class MyFormatter : IFormatProvider, ICustomFormatter
    {
        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            switch (format.ToUpper())
            {
                case "CUSTOM":
                    if (arg is short || arg is int || arg is long)
                        return arg.ToString();
                    if (arg is Single || arg is Double)
                        return String.Format("{0:0.00}",arg);
                    break;
                // Handle other
                default:
                    try
                    {
                        return HandleOtherFormats(format, arg);
                    }
                    catch (FormatException e)
                    {
                        throw new FormatException(String.Format("The format of '{0}' is invalid.", format), e);
                    }
            }
            return arg.ToString(); // only as a last resort
        }

        private string HandleOtherFormats(string format, object arg)
        {
            if (arg is IFormattable)
                return ((IFormattable)arg).ToString(format, CultureInfo.CurrentCulture);
            if (arg != null)
                return arg.ToString();
            return String.Empty;
        }

        public object GetFormat(Type formatType)
        {
            if (formatType == typeof(ICustomFormatter))
                return this;
            return null;
        }
    }
}

7
投票

这是 Uwe Keim 方法的替代方法,它仍然保持相同的方法调用:

var example1 = MyCustomFormat(123.1);  // Output: 123.10
var example2 = MyCustomFormat(123.95); // Output: 123.95
var example3 = MyCustomFormat(123);    // Output: 123

MyCustomFormat
类似于:

public static string MyCustomFormat( double myNumber )
{
    var str (string.Format("{0:0.00}", myNumber))
    return (str.EndsWith(".00") ? str.Substring(0, strLastIndexOf(".00")) : str;
}

7
投票

简单的一行代码:

public static string DoFormat(double myNumber)
{
    return string.Format("{0:0.00}", myNumber).Replace(".00","");
}

6
投票

试试这个:

var number = 123.4567;
var str = number.ToString("N2");

5
投票

恐怕没有内置格式可以做到这一点。您将必须使用不同的格式,具体取决于该值是否为整数。或者始终格式化为小数点后两位,然后操作字符串以删除任何尾随的“.00”。


5
投票

尝试:

String.Format("{0:0.00}", Convert.ToDecimal(totalPrice));

4
投票

如果其他答案都不适合您,可能是因为您在

ContentProperty
函数中绑定了控件的
OnLoad
,这意味着这不起作用:

private void UserControl_Load(object sender, RoutedEventArgs e)
{
  Bind.SetBindingElement(labelName, String.Format("{0:0.00}", PropertyName), Label.ContentProperty) 
}

解决方案很简单:xaml 中有一个

ContentStringFormat
属性。因此,当您创建标签时,请执行以下操作:

//if you want the decimal places definite
<Label Content="0" Name="labelName" ContentStringFormat="0.00"/>

或者

//if you want the decimal places to be optional
<Label Content="0" Name="labelName" ContentStringFormat="0.##"/>

3
投票

这样的东西也可以工作:

String.Format("{0:P}", decimal.Parse(Resellers.Fee)).Replace(".00", "")

3
投票

在处理来自 (T-)SQL 数据库的小数时,您希望能够转换具有 x 个小数位的可为空和不可为空的小数,并能够根据表定义轻松检查代码 - 当然,还可以显示向用户提供正确的小数位数。

不幸的是,实体框架不会自动将 SQL

decimal(18,2)
等内容转换为具有相同小数位数的 .NET 等价形式(因为只有完全精度的小数可用)。您必须手动截断小数位。

所以,我这样做了:

public static class Extensions
{
    public static string ToStringDecimal(this decimal d, byte decimals)
    {
        var fmt = (decimals>0) ? "0." + new string('0', decimals) : "0";
        return d.ToString(fmt);
    }

    public static string ToStringDecimal(this decimal? d, byte decimals)
    {
        if (!d.HasValue) return "";
        return ToStringDecimal(d.Value, decimals);
    }
}

用法示例:

void Main()
{
    decimal d = (decimal)1.2345;
    decimal? d2 = null; 

    Console.WriteLine(d.ToStringDecinal(2)); // prints: "1.23" (2 decimal places)
    Console.WriteLine(d.ToStringDecinal(0)); // prints: "1" (show integer number)
    Console.WriteLine(d2.ToStringDecimal(2)); // prints: "" (show null as empty string)
}

2
投票

为了使 Kahia 编写的代码更加清晰(很清楚,但当您想向其中添加更多文本时会变得很棘手)...尝试这个简单的解决方案。

if (Math.Round((decimal)user.CurrentPoints) == user.CurrentPoints)
     ViewBag.MyCurrentPoints = String.Format("Your current Points: {0:0}",user.CurrentPoints);
else
     ViewBag.MyCurrentPoints = String.Format("Your current Points: {0:0.0}",user.CurrentPoints);

我必须添加额外的转换(小数)才能让 Math.Round 比较两个小数变量。


1
投票

最近的一个项目有类似的要求。我写了这个十进制扩展方法, 它使用货币(“C”)格式说明符。除了删除零之外,它还具有小数位精度、货币符号、分隔符和文化选项。

public static DecimalExtension{

     public static string ToCurrency(this decimal val, 
                                     int precision = 2, 
                                     bool currencySymbol = false, 
                                     bool separator = false, 
                                     CultureInfo culture = null)
      {     
         if(culture == null) culture = new CultureInfo("en-US");
                                                   
         NumberFormatInfo nfi = culture.NumberFormat;
         nfi.CurrencyDecimalDigits = precision;
            
         string zeros = new String('0', precision);       
                    
         //Remove zeros
         var result = val.ToString("C",fi).Replace(nfi.CurrencyDecimalSeparator + zeros,"");
                     
         if(!separator) result = result.Replace(nfi.CurrencyGroupSeparator,"");
                    
         return currencySymbol? result: result.Replace(nfi.CurrencySymbol,"");      
        }   
}

示例:

decimal Total = 123.00M;
Console.WriteLine(Total.ToCurrency());  
//output: 123

decimal Total = 1123.12M;
Console.WriteLine(Total.ToCurrency()); 
//Output:  1123.12

Console.WriteLine(Total.ToCurrency(4));
//Output:  1123.1200

Console.WriteLine(Total.ToCurrency(2,true,true));
//output:  $1,123.12
 
CultureInfo culture = new CultureInfo("pt-BR")  //Brazil
Console.WriteLine(Total.ToCurrency(2,true,true, culture));
//output:  R$ 1.123,12

-1
投票

试试这个

string Output = String.Format("{0:0.00}", Decimal.Parse(InputStringValue));
© www.soinside.com 2019 - 2024. All rights reserved.