1

I wrote a code that will sort based on decimals. The quick sort algorithm. But my issue is the removal of the number 0 in some decimals. Eg 723.1000 becomes 723.1 Numbers are important to me because I want to be displayed and stored as

Now how can I do this without removing the zeros in decimal numbers

We may see these numbers in an array

Zeros will not be removed.

10.20 ==> 10.2

or

50.60000 ==> 50.6

or

145698.154780 ===> 145698.15478

So many zeros are not fixed

And this is my problem.

internal class Sorter
{
    public string[] QuickSort(string[] array, int low, int high)
    {
        if (low < high)
        {
            int p = Partition(array, low, high);
            QuickSort(array, low, p - 1);
            QuickSort(array, p + 1, high);
        }

        return array;
    }

    private static int Partition(string[] array, int low, int high)
    {
        int left = low + 1, right = high;
        string temp;
        double pivot = double.Parse(array[low]);
        int piv;

        while (left <= right)
        {
            while (left <= right && Convert.ToDouble(array[left]) <= pivot)


                left++;

            while (left <= right && Convert.ToDouble(array[right]) >= pivot)


                right--;

            if (left < right)
            {
                temp = array[left];
                array[left] = array[right];
                array[right] = temp;
            }
        }
        if (right == high)
            piv = high;
        else if (right == low)
            piv = low;
        else
            piv = left - 1;
        array[low] = array[piv];
        array[piv] = pivot.ToString();
        return piv;
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Martin
  • 386
  • 1
  • 5
  • 17
  • 1
    Couple issues: 1) trailing zeros do not affect the value itself, so it's not 100% what you are asking about; 2) you speak about decimals but use `double`, that's fundamentally different; 3) why do you keep parsing and coverting the values again and again? You should better convert `string[]` into `double[]` before sorting, and back to `string[]` after sorting. – Ondrej Tucny Dec 24 '13 at 10:05
  • Check the updated anwer – Mohammad Arshad Alam Dec 24 '13 at 16:22

4 Answers4

3

You can try with Double.ToString():

   double d=723.1000;
   string s = d.ToString("0.0");

Check here for more formatting options.

Update You can try this :

string s = d.ToString().TrimEnd('0');

Update 2: As discussed here :

double doesn't keep insignificant digits - there's no difference between 1.5 and 1.50000 as far as double is concerned.

If you want to preserve insignificant digits, use decimal instead. It may well be more appropriate for you anyway, depending on your exact context. (We have very little context to go on here...)

So you can use this decimal instead of double:

decimal d = 723.1000M;
string s = d.ToString();
Community
  • 1
  • 1
Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61
  • We may see these numbers in an array Zeros will not be removed. 10.20 ==> 10.2 or 50.60000 ==> 50.6 or 145698.154780 ===> 145698.15478 So many zeros are not fixed And this is my problem. – Martin Dec 24 '13 at 10:10
  • Answer is no different code The answer is both code 723.1 I want to display the number 723.1000. – Martin Dec 24 '13 at 13:47
1

try like this Custom Numeric Format Strings,

decimal d = 0.00000000000010000000000m;
string custom = d.ToString("0.#########################");
// gives: 0,0000000000001
string general = d.ToString("G29");
// gives: 1E-13
Ulaga
  • 863
  • 1
  • 8
  • 17
0

You may try some linq to sort something like

double[] myDubleNumber = { 15.00003,  1758.4856, 20.123, 1478.0214,120.0223 };

            var result = myDubleNumber.OrderBy(x => x).Select(x => Math.Round(x, 2));

            foreach (var d in result)
            {
                Console.WriteLine(d);
            }
            Console.ReadLine();
Binson Eldhose
  • 749
  • 1
  • 6
  • 14
0

It's better to be public double[] QuickSort(double[] array, int low, int high)

There should be 2 parts in your program:

  1. Input a numeric array and output the sorted one;
  2. Format and show the result.

In the first step, please don't consider about the removal of the number 0.

In the second step, please use .toString() here to format a number.

Anderson
  • 2,496
  • 1
  • 27
  • 41