Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6185671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:45:24+00:00 2026-05-24T01:45:24+00:00

In data formats where all underlying types are strings, numeric types must be converted

  • 0

In data formats where all underlying types are strings, numeric types must be converted to a standardized string format which can be compared alphabetically. For example, a short for the value 27 could be represented as 00027 if there are no negatives.

What’s the best way to represent a double as a string? In my case I can ignore negatives, but I’d be curious how you’d represent the double in either case.

UPDATE

Based on Jon Skeet’s suggestion, I’m now using this, though I’m not 100% sure it’ll work correctly:

static readonly string UlongFormatString = new string('0', ulong.MaxValue.ToString().Length);

public static string ToSortableString(this double n)
{
    return BitConverter.ToUInt64(BitConverter.GetBytes(BitConverter.DoubleToInt64Bits(n)), 0).ToString(UlongFormatString);
}

public static double DoubleFromSortableString(this string n)
{
    return BitConverter.Int64BitsToDouble(BitConverter.ToInt64(BitConverter.GetBytes(ulong.Parse(n)), 0));
}

UPDATE 2

I have confirmed what Jon suspected – negatives don’t work using this method. Here is some sample code:

void Main()
{
    var a = double.MaxValue;
    var b = double.MaxValue/2;
    var c = 0d;
    var d = double.MinValue/2;
    var e = double.MinValue;
    Console.WriteLine(a.ToSortableString());
    Console.WriteLine(b.ToSortableString());
    Console.WriteLine(c.ToSortableString());
    Console.WriteLine(d.ToSortableString());
    Console.WriteLine(e.ToSortableString());
}

static class Test
{
    static readonly string UlongFormatString = new string('0', ulong.MaxValue.ToString().Length);
    public static string ToSortableString(this double n)
    {
        return BitConverter.ToUInt64(BitConverter.GetBytes(BitConverter.DoubleToInt64Bits(n)), 0).ToString(UlongFormatString);
    }
}

Which produces the following output:

09218868437227405311
09214364837600034815
00000000000000000000
18437736874454810623
18442240474082181119

Clearly not sorted as expected.

UPDATE 3

The accepted answer below is the correct one. Thanks guys!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:45:25+00:00Added an answer on May 24, 2026 at 1:45 am

    Padding is potentially rather awkward for doubles, given the enormous range (double.MaxValue is 1.7976931348623157E+308).

    Does the string representation still have to be human-readable, or just reversible?

    That gives a reversible conversion leading to a reasonably short string representation preserving lexicographic ordering – but it wouldn’t be at all obvious what the double value was just from the string.

    EDIT: Don’t use BitConverter.DoubleToInt64Bits alone. That reverses the ordering for negative values.

    I’m sure you can perform this conversion using DoubleToInt64Bits and then some bit-twiddling, but unfortunately I can’t get it to work right now, and I have three kids who are desperate to go to the park…


    In order to make everything sort correctly, negative numbers need to be stored in ones-complement format instead of sign magnitude (otherwise negatives and positives sort in opposite orders), and the sign bit needs to be flipped (to make negative sort less-than positives). This code should do the trick:

    static ulong EncodeDouble(double d)
    {
        long ieee = System.BitConverter.DoubleToInt64Bits(d);
        ulong widezero = 0;
        return ((ieee < 0)? widezero: ((~widezero) >> 1)) ^ (ulong)~ieee;
    }
    
    static double DecodeDouble(ulong lex)
    {
        ulong widezero = 0;
        long ieee = (long)(((0 <= (long)lex)? widezero: ((~widezero) >> 1)) ^ ~lex);
        return System.BitConverter.Int64BitsToDouble(ieee);
    }
    

    Demonstration here: http://ideone.com/JPNPY

    Here’s the complete solution, to and from strings:

    static string EncodeDouble(double d)
    {
        long ieee = System.BitConverter.DoubleToInt64Bits(d);
        ulong widezero = 0;
        ulong lex = ((ieee < 0)? widezero: ((~widezero) >> 1)) ^ (ulong)~ieee;
        return lex.ToString("X16");
    }
    
    static double DecodeDouble(string s)
    {
        ulong lex = ulong.Parse(s, System.Globalization.NumberStyles.AllowHexSpecifier);
        ulong widezero = 0;
        long ieee = (long)(((0 <= (long)lex)? widezero: ((~widezero) >> 1)) ^ ~lex);
        return System.BitConverter.Int64BitsToDouble(ieee);
    }
    

    Demonstration: http://ideone.com/pFciY

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reporting services use format strings to auto format cell data. For example c2 formats
I used sprintf method to format data to a string which I want to
How can I format data coming from a DataBinder.Eval statement in an ASPX page?
I use a third party control which exports some data to different formats. The
I have webservice that can return data in multiple formats. For example json and
I have a SQL database table column which has data in this format 0000-00-0000
Im not sure how to output MySQL data into formats below. (eg: timelist, usersex,
I want to return the Model (data) of a controller in different formats (JavaScript/XML/JSON/HTML)
I'm developping a xml data format. Besides other data there should be a xaml
I would like to create my own data format for an iPhone app. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.