Concatenating longs and floats into a long

I have two numbers:

1234567890 <--- Long

and

0.123456 <--- Float

Is there any way to combine these to make a float(or double) in the following format:

(123)4567890.123456

I don't mind if the numbers in brackets have to be removed.

Solutions

Given a long l and a float f, you can use:

double result = l % 10000000 + (double) f;

This will usually lose some accuracy in the fraction portion.

Update: From a comment, we learn that these values are a time represented as a number of seconds and a fraction of a second and that it is desired to calculate an interval. If we want to find the difference between two times, then we can calculate the difference with fewer problems from accuracy and precision this way:

double SubtractTimes(long l0, float f0, long l1, float f1)
{
    long ld = l1 - l0;
    double fd = (double) f1 - f0;
    return ld + fd;
}

Note: If there is a concern that the time may have wrapped around some upper limit, then the code should test for this and make adjustments.

I must be missing something. Isn't it as easy as this?

long l = 1234567890;
float f = 0.123456;

float result = l + f;

Use this:

double result = l + f;
printf("%.6f",result);

Similar questions

Floats and Longs
I used sizeof to check the sizes of longs and floats in my 64 bit amd opteron machine. Both show up as 4. When I check limits.h and float.h for maximum float and long values these are the values I get: Since they both are of the same size, how can a float store such a huge value when compared to the long? I assume that they have a different storage...
How to put digits of a long long int into a long long int array
ok so I am trying to implement Client - server program (socket programming). My client send a long long int embedded in a string like this: SO send_data looks like Pre=278262617263 Now on the server side thus is what it looks like: now I want to convert this preMaster into the element of pre array. How do I go about doing that?
Packing Floats into a long long
I would like to pack 2 floats into an long long. What would be the correct way to do this?
Erratic behavior when using unsigned long longs in C/Python program
I found a bug in my C program that accepts a numpy array (as a PyObject*) and searches through it for a given value exceeding a threshold. Specifically, if the array is composed of 64-bit ints, the search gives an incorrect result and causes undefined behavior in my code. Here is a simplified version of my code (which supports a variety of array ty...
How to convert a (long *) to a 2 dimensional array of longs
I have the following code in C where G is a global variable: Once I store the address of matrix[0][0] in G, I can access all the elements of matrix like so: I want to be able to access all of these elements similar to how I can with matrix, by using the two square brackets from inside of another function. How would I be able to do that? That is, ho...
Dividing two extremely large numbers represented as a linked list of unsigned long longs
I am working in c and as said in the title, I have a struct that is a node for a doubly linked list of unsigned long longs that represent extremely large numbers. I need to implement basic arithmetic on them and have accomplished +,-,* but I still need division. I have seen answers for arithmetic on large numbers represented as linked lists but my ...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.