Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi, my problem is that. I tried to write number in a format. In this format, if this number is an integer only put after the digits.

Input Output
12.0 "12."
12.2 "12.2"

I tried
C#
String.Format("{0:0.###}", number))
, but it is not work. It convert integer number to string without dot notation.

Can you help me pls?

Thanks in advance.
Posted
Comments
Richard MacCutchan 28-May-13 11:39am    
Integers do not retain the fractional part of the number. You need to use the double type.

Read the documentation on String.Format and Custom Number Formats.

Replace the "#" characters with "0".
 
Share this answer
 
Comments
Richard MacCutchan 28-May-13 12:25pm    
# works fine.
Dave Kreskowiak 28-May-13 13:57pm    
Truthfully, I have no idea what he's really talking about. It's pretty impossible to understand, so I took a guess.
Richard MacCutchan 28-May-13 17:17pm    
What, you expected a clear description of the problem, complete with sample annotated code?
Dave Kreskowiak 28-May-13 18:35pm    
Nope, just a clear description of the problem. I'm not asking too much.

It floors me that if you can't describe what the code is supposed to do to another human, how on earth are you supposed to describe it to a computer, which a far more picky??
Richard MacCutchan 28-May-13 19:34pm    
Of course you are not asking too much. But in the real world, if people were able to explain their problem clearly, then I suspect there would be far fewer questions on this website.
I'm not aware of any culture settings that display a decimal point after a number that does not have any decimal places (I've been through the entire CultureInfo.GetCultures that are installed here). The closest you can get is with Dave Kreskoviak's solution of
String.Format("{0:0.0##}", number))
but that doesn't seem to be what you're after given your example above.

You will have to override the normal formatting process e.g.
C#
private string myDoubleToString(double d)
{
    if (d % 1 == 0)
        return d.ToString() + CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
    else
        return d.ToString();
}

or reconsider why you want the trailing decimal point in these circumstances - it's rather strange.
 
Share this answer
 
Comments
Mahmut Koyuncu 28-May-13 13:29pm    
Don't tell me. I rewrite a project which written in FORTRAN. It creates a file. And it must be in the same format in the C# project. And thanks for answer.
Mahmut Koyuncu 29-May-13 2:25am    
And maybe, Thread.CurrentThread.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator can used instead of CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator. I think it is a better solution.
CHill60 29-May-13 4:19am    
You're probably right - it doesn't hurt to think ahead! I'll be honest and say my first attempt just had + "." :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900