Python Forum
Storing float as integer in a database - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Storing float as integer in a database (/thread-4424.html)



Storing float as integer in a database - Ageir - Aug-15-2017

Hi! I'm rather new to programming in Python and trying to figure out how to store my sensor data from my RPi in a database. For example I have temperature sensors and humidity sensors. And I would like to store the readings with one decimal. To save space I'm storing these in the database as integer instead of float by adding the value by 10 and convert/round it to an int.

1. What is the proper name for this procedure? Must be a name out there for doing this.

2. What's the best pythonic way to do this conversion? Is there a built in feature for this?

Currently I'm doing it like this. But this is sooooooooo ugly! Please help! :)

   def to_db(self, value):
        ''' Rounds value to 1 decimal and multiplies by 10, returns integer.'''
        return int((round(value, 1) * 10))

    def from_db(self, value):
        ''' Converts db data from integer to float by dividing by 10.'''
        return (value / 10)
''' Storing to database '''
reading = DHT22(
    time=datetime.datetime.now(),
    temperature=self.to_db(self.dht22_internal.temperature),
    humidity=self.to_db(self.dht22_internal.humidity))
db_session.add(reading)



RE: Storing float as integer in a database - ichabod801 - Aug-15-2017

That looks fine to me. I would do it in a slight different order, but I don't think it would make a difference.


RE: Storing float as integer in a database - Ageir - Aug-15-2017

(Aug-15-2017, 05:10 PM)ichabod801 Wrote: That looks fine to me. I would do it in a slight different order, but I don't think it would make a difference.

Ah. How? And do you have any name suggestion for the functions?
Don't like the names I have, it's not clear enough what they are doing.
Any widely used terms for this that you know of?


RE: Storing float as integer in a database - ichabod801 - Aug-15-2017

I don't know about names. We did this at my old job with survey weights, but I'm not aware of any commonly used terms for it. How about a generalized form called int_power:

def int_power(n, pow):
    if pow >= 0:
        return int(round(n * 10 ** pow, 0))
    else:
        return n * 10 ** pow



RE: Storing float as integer in a database - sparkz_alot - Aug-16-2017

(Aug-15-2017, 04:25 PM)Ageir Wrote: To save space I'm storing these in the database as integer instead of float

Not sure what you mean by "save space". In Python 3.6 at least, you might be surprised:

Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.

C:\>python
Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getsizeof(5)
28
>>> sys.getsizeof(5.9)
24
>>> sys.getsizeof(1)
28
>>> sys.getsizeof(1.1)
24
>>> sys.getsizeof(100)
28
>>> sys.getsizeof(100.5)
24
>>> sys.getsizeof(0)
24
>>> sys.getsizeof(0.1)
24
>>>
As can be seen, an integer takes 28 bytes, while a float only takes 24 bytes.  Unless I'm misinterpreting your meaning or perhaps the usage of the sys.getsizeof() call.


RE: Storing float as integer in a database - ichabod801 - Aug-16-2017

I think he's worried about the size in the database, not the size in Python.


RE: Storing float as integer in a database - Ageir - Aug-17-2017

(Aug-16-2017, 03:43 PM)ichabod801 Wrote: I think he's worried about the size in the database, not the size in Python.

Yeah. I'm storing it in a SQL database. It gets quite big after a while.
Especially with a lot of sensors.