IT

문자열 형식을 사용하여 소수점 이하 2 자리 또는 간단한 정수 표시

lottoking 2020. 4. 1. 08:15
반응형

문자열 형식을 사용하여 소수점 이하 2 자리 또는 간단한 정수 표시


때로는 100 또는 100.99 또는 100.9 일 수있는 가격 필드가 표시됩니다. 원하는 것은 해당 가격에 소수를 입력 한 경우에만 소수점 이하 2 자리로 가격을 표시하는 것입니다. 예를 들어 100 인 경우에만 100을 100.00이 아니라 100으로 표시하고 가격이 100.2 인 경우 100.22와 유사하게 100.20을 표시해야합니다. 나는 googled하고 몇 가지 예를 보았지만 원하는 것과 정확히 일치하지 않았다.

// 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"

우아하지 않은 방법은 다음과 같습니다.

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;
    }
}

우아하지는 않지만 일부 프로젝트의 비슷한 상황에서 나를 위해 일하고 있습니다.


이 질문을 다시 활성화하여 죄송하지만 여기에서 정답을 찾지 못했습니다.

숫자 서식을 지정 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;
String.Format(number % 1 == 0 ? "{0:0}" : "{0:0.00}", number)

이것은 일반적인 형식의 부동 숫자 사용 사례입니다.

불행히도, 모든 내장 된 한 글자 형식 문자열 (예 : F, G, N)은이를 직접 달성하지 못합니다.
예를 들어 num.ToString("F2")항상 같은 소수점 이하 두 자리를 표시 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

오래된 질문이지만 내 의견으로는 가장 간단한 옵션을 추가하고 싶었습니다.

천 단위 구분 기호가 없는 경우 :

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 ;)
}

시험

double myPrice = 123.0;

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

어쨌든 형식 지정자에 조건을 넣을지는 모르지만 자신의 포맷터를 작성할 수 있습니다.

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;
        }
    }
}

다음은 동일한 메소드 호출을 유지하는 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;
}

간단한 한 줄 코드 :

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

이 작업을 수행 할 기본 제공 형식이없는 것이 두렵습니다. 값이 정수인지 여부에 따라 다른 형식을 사용해야합니다. 또는 항상 소수점 이하 2 자리로 서식을 지정하고 뒤에 문자열 ".00"을 제거하도록 문자열을 조작하십시오.


프로그램을 빠르게 실행해야하는 경우 $. {value : formatString} "및 string.Format (formatString, value)에 비해 문자열 형식화 성능이 ~ 35 % 빨라지도록 value.ToString (formatString)을 호출하십시오.

데이터

C # 문자열 형식화 성능-VS2017 15.4.5

암호

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

참고 문헌

사용자 지정 숫자 형식 문자열 [docs.microsoft.com]

Qt 차트 BarChart 예 [doc.qt.io]


다른 답변이 당신에게 도움 이되지 않으면 함수 ContentProperty의 컨트롤을 바인딩하고 있기 때문에 OnLoad이것이 작동하지 않을 수 있습니다.

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

해결책은 간단합니다. ContentStringFormatxaml에 속성이 있습니다. 따라서 레이블을 만들 때 다음을 수행하십시오.

//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.##"/>

이와 같은 것도 작동합니다.

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

시험:

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

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가 두 개의 십진 변수를 비교하도록 여분의 캐스트 (10 진수)를 추가해야했습니다.


이것은 나를 위해 일했다!

String amount= "123.0000";
String.Format("{0:0.##}", amount);      // "123.00"

참고 URL : https://stackoverflow.com/questions/6951335/using-string-format-to-show-decimal-up-to-2-places-or-simple-integer

반응형