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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:13:36+00:00 2026-05-26T18:13:36+00:00

I have to convert a double value x into two integers as specified by

  • 0

I have to convert a double value x into two integers as specified by the following…

“x field consists of two signed 32 bit integers: x_i which represents the integral part and x_f which represents the fractional part multiplied by 10^8. e.g.: x of 80.99 will have x_i as 80 and x_f as 99,000,000”

First I tried the following, but it seems to fail sometimes, giving an xF value of 1999999 when it ought to be 2000000

// Doesn't work, sometimes we get 1999999 in the xF
int xI = (int)x;
int xF = (int)(((x - (double)xI) * 100000000));

The following seems to work in all the cases that I’ve tested. But I was wondering if there’s a better way to do it without the round call. And also, could there be cases where this could still fail?

// Works, we get 2000000 but there's the round call
int xI = (int)x;
double temp = Math.Round(x - (double)xI, 6);
int xF = (int)(temp * 100000000);
  • 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-26T18:13:36+00:00Added an answer on May 26, 2026 at 6:13 pm

    The problem is (1) that binary floating point trades precision for range and (2) certain values, such as 3.1 cannot be repsented exactly in standard binary floating point formats, such as IEEE 754-2008.

    First read David Goldberg’s “What Every Computer Scientist Should Know About Floating-Point Arithmetic”, published in ACM Computing Surveys, Vol 23, No 1, March 1991.

    Then see these pages for more on the dangers, pitfalls and traps of using floats to store exact values:

    http://steve.hollasch.net/cgindex/coding/ieeefloat.html
    http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

    Why roll your own when System.Decimal gives you precise decimal floating point?

    But, if your going to do it, something like this should do you just fine:

    struct WonkyNumber
    {
        private const double SCALE_FACTOR    = 1.0E+8          ;
        private int          _intValue        ;
        private int          _fractionalValue ;
        private double       _doubleValue     ;
    
        public int    IntegralValue
        {
            get
            {
                return _intValue ;
            }
            set
            {
                _intValue = value ;
                _doubleValue = ComputeDouble() ;
            }
        }
        public int    FractionalValue
        {
            get
            {
                return _fractionalValue ;
            }
            set
            {
                _fractionalValue = value ;
                _doubleValue     = ComputeDouble() ;
            }
        }
        public double DoubleValue
        {
            get
            {
                return _doubleValue ;
            }
            set
            {
                this.DoubleValue = value ;
                ParseDouble( out _intValue , out _fractionalValue ) ;
            }
        }
    
        public WonkyNumber( double value ) : this()
        {
            _doubleValue = value ;
            ParseDouble( out _intValue , out _fractionalValue ) ;
        }
    
        public WonkyNumber( int x , int y ) : this()
        {
    
            _intValue        = x ;
            _fractionalValue = y ;
            _doubleValue     = ComputeDouble() ;
    
            return ;
        }
    
        private void ParseDouble( out int x , out int y )
        {
            double remainder = _doubleValue % 1.0 ;
            double quotient  = _doubleValue - remainder ;
    
            x = (int)   quotient                   ;
            y = (int) Math.Round( remainder * SCALE_FACTOR ) ;
    
            return ;
        }
    
        private double ComputeDouble()
        {
            double value =     (double) this.IntegralValue
                         + ( ( (double) this.FractionalValue ) / SCALE_FACTOR )
                         ;
            return value ;
        }
    
        public static implicit operator WonkyNumber( double value )
        {
            WonkyNumber instance = new WonkyNumber( value ) ;
            return instance ;
        }
    
        public static implicit operator double( WonkyNumber value )
        {
            double instance = value.DoubleValue ;
            return instance ;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function which should covert a double value into string one: inline
I have to convert an incoming String field into a BigDecimal field that would
I have some unicode codepoints (\u5315\u4e03\u58ec\u4e8c\u4e0a\u53b6\u4e4b), which I have to convert into actual characters
I'm trying to convert the following C# into F#: public class Matrix { double[,]
I have a double whose value is 10,000,000.00 (ten millions). I have to convert
I have a string like 1.5% and want to convert it to double value.
I have a double value 1318611498349.3559, I wanted to convert it to long by
can anyone show how to correctly convert binary represented data into double value in
Right now I have double numba = 5212.6312 String.Format({0:C}, Convert.ToInt32(numba) ) This will give
I often have to convert a retreived value (usually as a string) - and

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.