소수점 2 자리로 부동 소수점 서식 지정

현재 고객 웹 사이트를위한 판매 모듈을 구축하고 있습니다. 지금까지 완벽하게 계산할 판매 가격을 얻었지만 문제가 발생한 부분은 출력을 소수점 이하 2 자리로 포맷하는 것입니다.

현재이 결과를 목록보기에 데이터 바인딩 할 수 있도록 변수로 호출하고 있습니다.

Sale = float.Parse(((x.Sale_Price - (x.Sale_Price * (x.Discount_Price / 100))).ToString())),

누구나 소수점 둘째 자리로 출력 형식을 지정하는 방법을 보여줄 수 있습니까 ?? 많은 감사합니다!

197
задан mskfisher 5 June 2012 в 05:02
поделиться

1 ответ

Это для случаев, которые Вы хотите использовать интерполированные строки . Я на самом деле отправляю это, потому что я устал от метода проб и ошибок и в конечном счете просмотра тонн документов каждый раз, когда я должен отформатировать некоторый скаляр.

$"{1234.5678:0.00}"        "1234.57"        2 decimal places, notice that value is rounded
$"{1234.5678,10:0.00}"     "   1234.57"     right-aligned
$"{1234.5678,-10:0.00}"    "1234.57   "     left-aligned
$"{1234.5678:0.#####}"     "1234.5678"      5 optional digits after the decimal point
$"{1234.5678:0.00000}"     "1234.56780"     5 forced digits AFTER the decimal point, notice the trailing zero
$"{1234.5678:00000.00}"    "01234.57"       5 forced digits BEFORE the decimal point, notice the leading zero
$"{1234.5612:0}"           "1235"           as integer, notice that value is rounded
$"{1234.5678:F2}"          "1234.57"        standard fixed-point
$"{1234.5678:F5}"          "1234.56780"     5 digits after the decimal point, notice the trailing zero
$"{1234.5678:g2}"          "1.2e+03"        standard general with 2 meaningful digits, notice "e"
$"{1234.5678:G2}"          "1.2E+03"        standard general with 2 meaningful digits, notice "E"
$"{1234.5678:G3}"          "1.23E+03"       standard general with 3 meaningful digits
$"{1234.5678:G5}"          "1234.6"         standard general with 5 meaningful digits
$"{1234.5678:e2}"          "1.23e+003"      standard exponential with 2 digits after the decimal point, notice "e"
$"{1234.5678:E3}"          "1.235E+003"     standard exponential with 3 digits after the decimal point, notice "E"
$"{1234.5678:N2}"          "1,234.57"       standard numeric, notice the comma
$"{1234.5678:C2}"          "$1,234.57"      standard currency, notice the dollar sign
$"{1234.5678:P2}"          "123,456.78 %"   standard percent, notice that value is multiplied by 100
$"{1234.5678:2}"           "2"              :)

Производительность, Предупреждающая

, Интерполированные строки являются медленными. По моему опыту, это - порядок (быстро для замедления):

  1. value.ToString(format)+" blah blah"
  2. string.Format("{0:format} blah blah", value)
  3. $"{value:format} blah blah"
0
ответ дан 23 November 2019 в 05:13
поделиться
Другие вопросы по тегам:

Похожие вопросы: