19

I have a decimal number, say 1234.500.

I want to display it as 1,234.5.

I'm currently converting it to a double to remove the trailing '0's.

string.Format("{0:0,0}",1234.500) removes the decimal place, and other formatting options seem to use two decimal places regardless.

Can anyone offer insight?

4
  • Edit, just for clarification 1234.560, i would expect 1,234.56 Commented Oct 19, 2009 at 9:44
  • You wanna display it as 1,234.**5** right? Edit the post to reflect that. Commented Oct 19, 2009 at 9:48
  • Do you want to use local system settings or just force the application to format it this way? Commented Oct 19, 2009 at 9:50
  • What does regex have to do with this? Commented Oct 19, 2009 at 10:59

6 Answers 6

28

You should use a custom formatting like #,##0.00

string s = string.Format("{0:#,##0.00}", xx);

Will produce 1,234.50 when xx = 1234.5M
forget about converting to double, that won't really help.

Sign up to request clarification or add additional context in comments.

5 Comments

Dont want any trailing zero's
Then remove 1 or more zeroes: #,##0. or make them optional: #,##0.#
Also, decimal, per IEEE standard SHOULD format to string (unless given other format providers) with shown/kept zeroes after the decimal point.
can this be made to work for different cultures / locale settings too?
@Califf - of course it can. Look st the list of overloads.
11

Are you aware that .NET has built-in support for correctly formatting numbers according the the regional settings of each user of your application? It might be better to leverage the user's own regional settings (the .NET framework knows all the right settings already).

However, if you want to fix your application to format numbers in a particular regional setting, you can still leveage a particular locale of your choice and force .NET to use that as the basis of all formatting (not just numbers):

using System.Globalization;
using System.Threading;
...
CultureInfo us = new CultureInfo("en-US");

and then

Thread.CurrentThread.CurrentUICulture = us;

or just

Console.WriteLine(d.ToString("c", us));

You should be aware that the use of commas as a thousands separator is appropriate for UK and USA but it is not how thousands should be displayed in other countries

"one thousand and twenty-five is displayed as 1,025 in the United States and 1.025 in Germany. In Sweden, the thousands separator is a space"

MSDN has dedicated section devoted to this topic which they call 'Globalization' (i.e. that's a good search term if ever you need to hunt down more detail). That page describes how the use of the pound sign works as a digit placeholder for removing the trailing zeros (as suggested by Henk Holterman in a previous comment).

See also Custom Numeric Format Strings.

Comments

9

Another alternative:

decimal d = 1234.56m;
string s = d.ToString("N");    // 1,234.56

Comments

0

Have you tried this?

   String.Format("{0:0,0.00}", 1234.560);

1 Comment

Doesnt remove trailing zero's from decimal - try String.Format("{0:0,0.00}", 1234.5)
0

try this !

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Try
            TextBox1.Text = Format(CInt(TextBox1.Text), "#,#")
            TextBox1.SelectionStart = TextBox1.Text.Length
        Catch ex As Exception

        End Try

End Sub

Comments

0

Try this:

string myNumber = string.Format("{0:#,0.00}", 1234.500);

(I've seen this in RDLC reports)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.