Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sane way to convert a double to a unit #24

Open
narrowtux opened this issue Jun 26, 2019 · 3 comments
Open

Sane way to convert a double to a unit #24

narrowtux opened this issue Jun 26, 2019 · 3 comments

Comments

@narrowtux
Copy link

narrowtux commented Jun 26, 2019

Right now, the only working way to convert a double precision value you know the unit of into a value of the type unit is concat(23.5, '°C')::unit which is a bit clunky.

I tried 23.5 * '°C'::unit but this does not work with units with offsets (such as celsius) as this would return the incorrect amount 23.5 K.

I'd imagine a function like at_unit(double precision, unit) which might be able to do the job more efficiently instead of converting a double to string and back again.

@df7cb
Copy link
Owner

df7cb commented Jun 26, 2019

The generic way for non-shifted units would be multiplication.
At the moment there's only two shifted unit supported (°C and °F, the other in definitions.units would be various shoe sizes which I didn't bother importing). For °C, there's a predefined constructor function (like for many of the standard SI units like meter(), ...):

# select celsius(23.5);
 celsius  
──────────
 296.65 K

There's none for °F, and adding a generic one makes sense. The signature will have to be at_unit(double precision, text) though, because anything in "unit" gets converted to base units immediately so the function wouldn't even see any shift.

@publicmatt
Copy link

Bump. I love this package, but getting 'real-world' data into this domain is tricky.

@df7cb
Copy link
Owner

df7cb commented Jun 20, 2023

For any "normal" unit you can use multiplication:

insert into currents values (123.456 * 'mA'::unit);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants