yu yang Jian
yu yang Jian

Reputation: 7171

C# How to nicely format floating numbers to String without unnecessary decimal 0?

I have the same problem as HERE, but I'm using C#,

How to do it in C#?

(if use Tostring("F") as Here, all float number will turn into X.XX, also 0 to 0.00)

Here's an example float numbers

232.00000000
0.18000000000
1237875192.0
4.5800000000
0.00000000
1.23450000

What I want to turn them into:

232
0.18
1237875192
4.58
0
1.2345

Edit

(I suddenly find what I want to do is more complicate than above but it's too late to modify the question, maybe I'll ask it in another question...)

Upvotes: 10

Views: 20542

Answers (5)

eldercharlie
eldercharlie

Reputation: 46

try: decimalValue.ToString("0");

Upvotes: 0

Jcl
Jcl

Reputation: 28272

You can use the 0.############ format. Add as many # as decimal places you think you may have (decimals will be rounded off to those many places):

string output = number.ToString("0.############");  

Sample fiddle: https://dotnetfiddle.net/jR2KtK

Or you can just use the default ToString(), which for the given numbers in en-US culture, should do exactly what you want:

string output = number.ToString();  

Upvotes: 12

Darshan Faldu
Darshan Faldu

Reputation: 1601

You have to create your own extension method something link this....

Extension Method

namespace myExtension
{
    public static class myMath
    {
        public static double myRoundOff(this double input)
        {
            double Output;

            double  AfterPoint = input - Math.Truncate(input);
            double  BeforePoint = input - AfterPoint;

            if ((Decimal)AfterPoint == Decimal.Zero && (Decimal)BeforePoint == Decimal.Zero)
                Output = 0;

            else if ((Decimal)AfterPoint != Decimal.Zero && (Decimal)BeforePoint == Decimal.Zero)
                Output = AfterPoint;

            else if ((Decimal)AfterPoint == Decimal.Zero && (Decimal)BeforePoint != Decimal.Zero)
                Output = BeforePoint;

            else
                Output = AfterPoint + BeforePoint;

            return Output;
        }
    }
}

Call your Extension Method

using myExtension;

namespace yourNameSpace
{
    public partial class YourClass
    {
        public void YourMethod
        {
            double d1 = 232.00000000.myRoundOff();  // ANS -> 232
            double d2 = 0.18000000000.myRoundOff(); // ANS -> 0.18
            double d3 = 1237875192.0.myRoundOff();  // ANS -> 1237875192
            double d4 = 4.5800000000.myRoundOff();  // ANS -> 4.58
            double d5 = 0.00000000.myRoundOff();    // ANS -> 0
            double d6 = 1.23450000.myRoundOff();    // ANS -> 1.2345
        }
    }
}

Upvotes: 1

user3345546
user3345546

Reputation: 11

Use the String.Format() method to remove trailing zeros from floating point numbers. for example:

        float num = 23.40f;
        Console.WriteLine(string.Format("{0}",num));

It prints 23.4

Upvotes: 1

Amit Kumar Ghosh
Amit Kumar Ghosh

Reputation: 3726

you should use int.ToString("G") overload.

Read this - https://msdn.microsoft.com/en-us/library/8wch342y(v=vs.110).aspx

Upvotes: -1

Related Questions