1

I'm trying different format for a price value in an application (ASP.NET MVC 4), first I tested below code in LINQPad and it worked well (no meaningless trailing zeros). But when I put these code, string.Format("G", value) specifically in my code, it didn't work, trailing zeros appear again!!! Why?

LINQPad snippet:

decimal d = 0.3M, d2 = 0.45M, d3 = 13.2M, d4 = 1049M, d5 = 12492.4M, d6 = 2000M;

var culture = CultureInfo.CreateSpecificCulture("vi-VN");

Console.WriteLine(string.Format(culture, "{0:G}", d));
Console.WriteLine(string.Format(culture, "{0:G}", d2));
Console.WriteLine(string.Format(culture, "{0:G}", d3));
Console.WriteLine(string.Format(culture, "{0:G}", d4));
Console.WriteLine(string.Format(culture, "{0:G}", d5));
Console.WriteLine(string.Format(culture, "{0:G}", d6));

LINQPad output:

// Output:
// 0,3
// 0,45
// 13,2
// 1049
// 12492,4
// 2000

My application output:

// Output:
// 0,30
// 0,45
// 13,20
// 1049,00
// 12492,40
// 2000,00

UPDATE 1: I tried again following code in a .NET 4 console app, and a ASP.NET MVC 4 (both .NET 4 and .NET 4.5) and all worked well (no zeros). But it still did not work in my application, even when I put them in an empty action. The only different is my application (ASP.NET MVC 4) was created in VS2010 + .NET 4 and upgraded to VS2012 + .NET 4.5 later. Is it issue?

decimal d = 0.3M;
string s = string.Format(CultureInfo.CreateSpecificCulture("vi-VN"), "{0:G}", d);
Tien Do
  • 10,319
  • 6
  • 41
  • 42
  • Are you displaying the output in your ASP.NET page or writing to the console? – D Stanley Oct 03 '12 at 02:23
  • Also are you positive you're using the latest code on your site? i.e. you've re-build the project. Add some debugging lines to verify, like `Debug.WriteLine(culture.NumberFormat.NumberDecimalDigits);` – D Stanley Oct 03 '12 at 02:38
  • @D Stanley: I were asking for this issue in my ASP.NET MVC 4 code, and above code is just a quick example, that using same format string {0:G} – Tien Do Oct 03 '12 at 15:12
  • The general format uses values from System.Globalization.NumberFormatInfo to determine some of how it parses numbers. I'd check there for differences and see if, for example, you aren't setting NumberDecimalDigits, somewhere. – Jacob Proffitt Oct 03 '12 at 15:45
  • @Jacob: NumberDecimalDigits is 2 so 16 is outputted as 16,00 is correct, right? But I still don't know what made this difference in my application. Thanks. – Tien Do Oct 11 '12 at 18:09
  • I'd take that as a starting point to start poking at it. Since NumberFormatInfo is culturally dependent, I'd check the behavior out under different cultures (including the InvariantInfo). – Jacob Proffitt Oct 11 '12 at 20:48

2 Answers2

0

Use {0:G29} instead

decimal d = 0.3M, d2 = 0.45M, d3 = 13.2M, d4 = 1049M, d5 = 12492.4M, d6 = 2000M;

var culture = CultureInfo.CreateSpecificCulture("vi-VN");

Console.WriteLine(string.Format(culture, "{0:G29}", d));
Console.WriteLine(string.Format(culture, "{0:G29}", d2));
Console.WriteLine(string.Format(culture, "{0:G29}", d3));
Console.WriteLine(string.Format(culture, "{0:G29}", d4));
Console.WriteLine(string.Format(culture, "{0:G29}", d5));
Console.WriteLine(string.Format(culture, "{0:G29}", d6));

Reference: Remove trailing zeros

Community
  • 1
  • 1
Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
  • 2
    But why? I tried the OP's code in a brand new console application and it did *not* add trailing zeros. – Kirk Woll Oct 03 '12 at 02:19
  • 1
    The OP is not asking _how_ to remove the trailing zeros, he/she is asking why they're there in the first place. – D Stanley Oct 03 '12 at 02:24
  • I have given a replacement if {0:G} is not working, now let OP decide or comment here. – Furqan Safdar Oct 03 '12 at 02:32
  • Thanks, but it is true that I were not asking for removing trailing zeros. Actually I could do it with {0:0.##} by looked at related posts on SO. But {0:G} is still an issue in my application. – Tien Do Oct 03 '12 at 15:17
0

It seems that this is just my application problem, not ToString("G"). Thanks for reading.

Tien Do
  • 10,319
  • 6
  • 41
  • 42