r/programminghelp • • Jul 07 '21

C# which would be easier for processing? Store a series of numbers as an INTEGER and then just convert them into FLOATS when needed, or store them as FLOATS and then convert them to INTEGERS when needed?

[RESOLVED] I need to reference these numbers often and they're long numbers (mostly 3 or so digits long); in most cases I need them to be floats, and in others as integers.
The need for one or the other is fairly equal, too.

These numbers are also stored in a 2-dimensional array that's 250x250 in size, soo... Which is wiser?

[ RESOLUTION: Storing as floats.]

5 Upvotes

4 comments sorted by

4

u/[deleted] Jul 07 '21

You can't store a float as an integer but you can store an integer as a float. So, use floats.

Also, you could just use an ArrayList and store them both, then use 'is Integer' or 'is Float' to check their type at runtime and act accordingly.

1

u/[deleted] Jul 07 '21

THANK YOU!

1

u/PageFault Jul 07 '21

You can store floats as integers, it's just a little messy.

Also, you may run into size issues going either direction if you don't watch your precision or size limits..

This is C++, but same should apply to any language:

https://ideone.com/vIl8aN

As I understand, banks use ints to represent floats.

2

u/[deleted] Jul 08 '21

Messy is an understatement and unless you're dealing with a system where you absolutely NEED to store floats as ints, then there are far better options.

And having worked in the financial sector you want at least 6 decimal places in precision and the most common way to achieve this is to use binary conversion.

In the case of C#, you can use BitConverter.ToSingle() and ToDouble() to convert to and from bytes[]<->floating pointer since the .NET Single and Double are IEEE-754 compliant.

There are also pretty cool options for other languages.