forallpeople icon indicating copy to clipboard operation
forallpeople copied to clipboard

Changing unit

Open DiegoTheWolf opened this issue 3 years ago • 6 comments

I made a new json which includes

"cm": {
    "Dimension": [0,1,0,0,0,0,0],
    "Value": 0.01},
"mm": {
    "Dimension": [0,1,0,0,0,0,0],
    "Value": 0.001},

now i have a calculation of m * cm * mm and it's result is . when i leave out the m i get a result of mm².

On what basis is the resulting unit calculated.

And how can i change it? (i want cm in both lines)

DiegoTheWolf avatar Oct 27 '21 15:10 DiegoTheWolf

I have the same problem. In my notebook, some forces are displayed as kN (F1 = 674 kN) and others as MN (F2 = 1.508 MN). Can I explicitely convert/require F2 to be in kN (for a more straightforward comparison)? It would be nice to have such an option in the library.

eagle-tex avatar Nov 21 '21 10:11 eagle-tex

Hi @DiegoTheWolf and @eagle-tex,

There is a method called .prefix() that accepts a string prefix to be "forced" upon the unit so that it always displays that way. It works like this:

a = 4 * m
b = 200 *mm
c = (a+b).prefix('c')
c

420.000 cm

The same would work as follows:

F2 = 1508*kN
F2

1.508 MN

F2.prefix('k')

1508 kN

connorferster avatar Dec 03 '21 22:12 connorferster

@connorferster

Thanks a lot for the reply. I tried the code you provided and it worked... to a point.

L = 3347.526*m

When I try to display L, it shows 3.348 km (automatic conversion in km) a.prefix("") is supposed to display L back into m (meters). But for some reason, it does not work.

Do you have any idea why and what could be done to solve that problem?

Btw, on the GitHub page of forallpeople, the method prefix does not exist. prefixed exists but that one doesn't work. That was probably a typo in the markdown file.

eagle-tex avatar Dec 04 '21 00:12 eagle-tex

Hi @eagle-tex,

Ah, yes this part is less obvious. For specifying no prefix, as follows:

L = 3347.526*m
a = L.prefix('unity')

I need to update the documentation, badly. Ideally with a proper readthedocs site. Coming soon. Will update the Github page in the mean time.

connorferster avatar Dec 04 '21 00:12 connorferster

Hi @connorferster At this purpose, you can just assign the prefix default value with the prefix of creation?

I mean, instead of doing this:

%%render param
f = 5000*si.kN.prefix('k')

it automatically add the prefix k since I defined f with kN.

And maybe adding a switch with this behaviur like you did in HandCalcs automatically_convert_units_over_1000 True | False

sononicola avatar Aug 20 '22 08:08 sononicola

@sononicola

It actually does this with all SI units that are a power of 1000 between 1e24 and 1e-24. The behavior is not so much defined by the creation of the unit with kN but more because 1 kN is actually 1000 N and it will "auto-prefix" the value because of this.

One way to manage this would be to have a toggle to just auto-prefixing on and off.

connorferster avatar Aug 31 '22 05:08 connorferster