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 6864145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:52:28+00:00 2026-05-27T02:52:28+00:00

I have a double which has actual value (as seen from the debugger) val

  • 0

I have a double which has actual value (as seen from the debugger) val = 1.5530000000000002

however if I print it like string.Format("{0}", val) I get “1.553” which is realistic value I am looking for.

the 1.5530000000000002 comes from some socket API and I cant change that. But I need 1.553..

what shall I do?

Please note I receive thousands of these per sec. I can’t afford tinkering with the Math class or converting them forth and back. any consolations?

  • 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-27T02:52:29+00:00Added an answer on May 27, 2026 at 2:52 am

    I decided to upscale them to ints. I can do it because I know how many significant digits are in the number.

    int real = (int)(dbl * multiplier);
    

    I am sure decimals will be slower simply because I dont have x128 CPU and memory bus. Using Math is out questions ever.

    sorry for the blur with casting to float earlier.

    EDIT OK. So I got that SO now is not free anymore. Each question need now attention which is costly in time… Here is the code which answers why I will not use Decimals and Math.

    class Program
    {
        static void Main(string[] args)
        {
            Start();
            Console.ReadLine();                
        }
    
        static int TESTS = 10;
        static int LOOPS = 50000;
    
        static double d1 = 1.5530000000000002;
        static double d2 = 1.5531;
    
        static double dResult;
        static int iResult;
        static decimal cResult;
    
        static void Start()
        {
            // actual test
            for (int x = 0; x < TESTS; x++)
            {
                Stopwatch sw = new Stopwatch();
    
                long tick1, tick2, tick3;
                sw.Start();
                for (int j = 0; j < LOOPS; j++)
                {
                    dResult = Math.Round(d1 / 2.0, 4);
                    dResult = Math.Round(d2 / 2.0, 4);
                }
                sw.Stop();
    
                tick1 = sw.ElapsedTicks;
    
                sw.Restart();
                for (int j = 0; j < LOOPS; j++)
                {
                    iResult = (int)(d1 / 2.0 * 10000.0);
                    iResult = (int)(d2 / 2.0 * 10000.0);
                }
                sw.Stop();
    
                tick2 = sw.ElapsedTicks;
    
                sw.Restart();
                for (int j = 0; j < LOOPS; j++)
                {
                    cResult = decimal.Round((decimal)d1, 4);
                    cResult = decimal.Round((decimal)d2, 4);
                }
                sw.Stop();
    
                tick3 = sw.ElapsedTicks;
    
                Console.WriteLine("Math {0} Int {1} Decimal {2}", tick1, tick2, tick3);
            }
        }
    }
    

    first part – is Math, second is what I am intended to use (conversion to int), and the third is Decimals.

    The results on my i7 as follows – 12K ticks, 700 ticks, 35K ticks respectively.

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

Sidebar

Related Questions

I have a Double which could have a value from around 0.000001 to 1,000,000,000.000
In C#. I have a double (which I've extracted from a database) that has
i have a c function which returns a long double . i'd like to
I have a double value f and would like a way to nudge it
I have a listbox which has couple of items. When double clicked on each
I have an object which has one field- double[] _myField it's hashcode is public
I have a typedef boost::variant<int, float, double, long, bool, std::string, boost::posix_time::ptime> variant which I
I have this class which has a double list template of a struct of
I have an controller which has check like that if (form[submit].ToString() == Continue) {
I want to double buffer a custom control which contains buttons. I have tried

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.