ItGo.me Focus on IT Recommend

Home > .net - How to convert between "#,##0" and "{0:n2}" style format string representations?

.net - How to convert between "#,##0" and "{0:n2}" style format string representations?

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

up vote 0 down vote favorite 1 .NET supports two types of string formatting. I'm in a situation where existing configuration data has #,##0 style formatting. A new feature requires formatting to the same output, but the API needed for this feature only accepts formatting of type {0:n2}. Does anyone know of a means to convert between these two representations for numeric types? DateTime can be ignored. EDIT I've learned that: The {0:n2} style is known as standard numeric formatting The #,##0 style is known as custom numeric formatting .net formatting string-formatting
  edited Feb 20 '09 at 12:33 asked Feb 20 '09 at 11:36 Drew Noakes 141k 84 427 516



 |  3 Answers

up vote 2 down vote ---Accepted---Accepted---Accepted---

No, you can't. From your link to the MSDN articles about standard format strings, you'll find: The actual negative number pattern, number group size, thousand separator, and decimal separator are specified by the current NumberFormatInfo object. So the standard formatting specifiers will vary depending on which culture the program is running under. Since your custom formating specifies exactly how the number is going to look, whatever the culture the program is running under. Its allways gonna look the same. The culture the program is running under isn't known during compile time, it's a runtime property. So the answer is: No, you can't map automatically, because there isn't a one-to-one consistent mapping.
  edited Feb 20 '09 at 13:00 answered Feb 20 '09 at 12:43 Arjan Einbu 10.5k 37 55      @Arjan, dang. I wasn't really thinking outside my locale as it's an in-house app, but thanks for explaining this. The purpose of these different formats is clear to me now. Great answer. –  Drew Noakes Feb 20 '09 at 13:24



 

Recommend:Using .NET string formatting, how do I format a string to display blank (empty string) for zero (0)

applies to String formatting in .NET in general. The customer has requested that if the value of a string is 0, it should not be displayed. I have the following hack to accomplish this: <%# IIf(DataBinder.Eval(Container.DataItem, "MSDWhole

;|  up vote 0 down vote HACK ALERT!!! As Arjan pointed out in his excellent answer what I want to do isn't possible in a bullet proof fashion across all locales (Thanks Arjan). For my purposes, I know I'm only dealing with numbers and the major thing I'm concerned with is having the same number of decimal places. So here's my hack. private static string ConvertCustomToStandardFormat(string customFormatString)

{

if (customFormatString == null || customFormatString.Trim().Length == 0)

return null;

// Percentages do not need decimal places

if (customFormatString.EndsWith("%"))

return "{0:P0}";

int decimalPlaces = 0;

int dpIndex = customFormatString.LastIndexOf('.');

if (dpIndex != -1)

{

for (int i = dpIndex; i < customFormatString.Length; i++)

{

if (customFormatString[i] == '#' || customFormatString[i] == '0')

decimalPlaces++;

}

}

// Use system formatting for numbers, but stipulate the number of decimal places

return "{0:n" + decimalPlaces + "}";

}


  answered Feb 20 '09 at 13:28 Drew Noakes 141k 84 427 516



 |  up vote 0 down vote This used for formatting number into 2 decimal places string s = string.Format("{0:N2}%", x);


  answered Mar 7 '16 at 16:17 Apps Tawale 154 1 5



 | 

Recommend:.net - How to convert between "#,##0" and "{0:n2}" style format string representations

tion data has #,##0 style formatting. A new feature requires formatting to the same output, but the API needed for this feature only accepts formatting of type {0:n2}. Does anyone know of a means to convert between these two representations

up vote 0 down vote favorite 1 .NET supports two types of string formatting. I'm in a situation where existing configuration data has #,##0 style formatting. A new feature requires

------splitte line----------------------------