Mr *_*r A 266 c# regex string-formatting number-formatting
我有一个显示的价格字段,有时可以是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"
Run Code Online (Sandbox Code Playgroud)
Gh6*_*h61 485
很抱歉重新启动此问题,但我在这里找不到正确的答案.
在格式化数字中,您可以将其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"
Run Code Online (Sandbox Code Playgroud)
你也可以结合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"
Run Code Online (Sandbox Code Playgroud)
因为总是使用这种格式化方法CurrentCulture.对于一些文化.将改为,.
Uwe*_*eim 145
一种不雅的方式是:
var my = DoFormat(123.0);
Run Code Online (Sandbox Code Playgroud)
有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;
}
}
Run Code Online (Sandbox Code Playgroud)
不优雅,但在某些项目的类似情况下为我工作.
det*_*ale 58
这是一种常见的格式化浮点数用例.
不幸的是,所有内置的单字母格式字符串(例如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
Run Code Online (Sandbox Code Playgroud)
And*_*rew 39
老问题,但我想在我看来添加最简单的选项.
没有千位分隔符:
value.ToString(value % 1 == 0 ? "F0" : "F2")
Run Code Online (Sandbox Code Playgroud)
有数千个分隔符:
value.ToString(value % 1 == 0 ? "N0" : "N2")
Run Code Online (Sandbox Code Playgroud)
与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
Run Code Online (Sandbox Code Playgroud)
如果你在很多地方需要它,我会在扩展方法中使用这个逻辑:
public static string ToCoolString(this decimal value)
{
return value.ToString(value % 1 == 0 ? "N0" : "N2"); // Or F0/F2 ;)
}
Run Code Online (Sandbox Code Playgroud)
Yah*_*hia 27
尝试
double myPrice = 123.0;
String.Format(((Math.Round(myPrice) == myPrice) ? "{0:0}" : "{0:0.00}"), myPrice);
Run Code Online (Sandbox Code Playgroud)
无论如何我都不知道在格式说明符中添加条件,但是你可以编写自己的格式化程序:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是Uwe Keim方法的替代方法,它仍将保持相同的方法调用:
var example1 = MyCustomFormat(123.1); // Output: 123.10
var example2 = MyCustomFormat(123.95); // Output: 123.95
var example3 = MyCustomFormat(123); // Output: 123
Run Code Online (Sandbox Code Playgroud)
有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;
}
Run Code Online (Sandbox Code Playgroud)
简单的一行代码:
public static string DoFormat(double myNumber)
{
return string.Format("{0:0.00}", myNumber).Replace(".00","");
}
Run Code Online (Sandbox Code Playgroud)
如果您的程序需要快速运行,则调用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;
}
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
Custom Numeric Format Strings [docs.microsoft.com]
Qt Charts BarChart Example [doc.qt.io]
小智 6
尝试这个:
var number = 123.4567;
var str = number.ToString("N2");
Run Code Online (Sandbox Code Playgroud)
小智 5
尝试:
String.Format("{0:0.00}", Convert.ToDecimal(totalPrice));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
658739 次 |
| 最近记录: |