Python Forum
Storing float as integer in a database
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Storing float as integer in a database
#1
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)
Reply
#2
That looks fine to me. I would do it in a slight different order, but I don't think it would make a difference.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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?
Reply
#4
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
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(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.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#6
I think he's worried about the size in the database, not the size in Python.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python calculate float plus float is incorrect? sirocawa 6 136 7 hours ago
Last Post: DeaD_EyE
  How to check if user entered string or integer or float?? prateek3 5 11,325 Dec-21-2019, 06:24 PM
Last Post: DreamingInsanity
  Converting float (2.0) to integer(2) Raj_Kumar 2 2,502 Dec-07-2019, 11:01 AM
Last Post: DeaD_EyE
  Comaparing Float Values of Dictionary Against A Float Value & Pick Matching Key firebird 2 3,374 Jul-25-2019, 11:32 PM
Last Post: scidam
  storing input as a integer help faputting 1 2,434 Feb-23-2018, 06:39 PM
Last Post: nilamo
  cannot convert float infinity to integer error despite rounding off floats Afterdarkreader 3 15,188 Dec-15-2017, 02:01 AM
Last Post: micseydel
  Taking user input and storing that to a variable then storing that variable to a list jowalk 12 37,314 Mar-27-2017, 11:45 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020