Skip to content

Commit 8553b16

Browse files
Add quantiphy, print_results() uses engineering notation
1 parent b0b1185 commit 8553b16

File tree

7 files changed

+417
-125
lines changed

7 files changed

+417
-125
lines changed

docs/examples/area_properties.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
"outputs": [],
133133
"source": [
134134
"gross_props = conc_sec.get_gross_properties()\n",
135-
"gross_props.print_results(fmt=\".3e\")"
135+
"gross_props.print_results()"
136136
]
137137
},
138138
{
@@ -153,7 +153,7 @@
153153
"outputs": [],
154154
"source": [
155155
"transformed_props = conc_sec.get_transformed_gross_properties(elastic_modulus=30.1e3)\n",
156-
"transformed_props.print_results(fmt=\".3e\")"
156+
"transformed_props.print_results()"
157157
]
158158
}
159159
],

docs/examples/prestressed_section.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@
270270
"source": [
271271
"gross_props_serv = conc_sec_serv.get_gross_properties()\n",
272272
"gross_props_ult = conc_sec_ult.get_gross_properties()\n",
273-
"gross_props_serv.print_results(fmt=\".3e\")"
273+
"gross_props_serv.print_results()"
274274
]
275275
},
276276
{
@@ -376,7 +376,7 @@
376376
"source": [
377377
"cr_p_serv = conc_sec_serv.calculate_cracked_properties(m_ext=0)\n",
378378
"cr_p_ult = conc_sec_ult.calculate_cracked_properties(m_ext=0)\n",
379-
"cr_p_serv.print_results(fmt=\".3e\")\n",
379+
"cr_p_serv.print_results()\n",
380380
"cr_stress_p_serv = conc_sec_serv.calculate_cracked_stress(cracked_results=cr_p_serv)\n",
381381
"cr_stress_p_serv.plot_stress()"
382382
]

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ dependencies = [
4040
"cytriangle~=1.0.3",
4141
"rich[jupyter]~=13.9.3",
4242
"more-itertools~=10.5.0",
43+
"quantiphy~=2.20",
4344
]
4445

4546
[project.urls]

src/concreteproperties/post.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import TYPE_CHECKING
77

88
import matplotlib.pyplot as plt
9+
from quantiphy import Quantity
910

1011
if TYPE_CHECKING:
1112
import matplotlib.axes
@@ -100,3 +101,41 @@ def plotting_context(
100101
else:
101102
plt.draw()
102103
plt.pause(0.001)
104+
105+
106+
def string_formatter(
107+
value: float,
108+
eng: bool,
109+
prec: int,
110+
scale: float = 1.0,
111+
) -> str:
112+
"""Formats a float using engineering or fixed notation.
113+
114+
Args:
115+
value: Number to format
116+
eng: If set to ``True``, formats with engineering notation. If set to ``False``,
117+
formats with fixed notation.
118+
prec: The desired precision (i.e. one plus this value is the desired number of
119+
digits)
120+
scale: Factor by which to scale the value. Defaults to ``1.0``.
121+
122+
Returns:
123+
Formatted string
124+
"""
125+
q = Quantity(value)
126+
form = "eng" if eng else "fixed"
127+
val_fmt = q.render(
128+
form=form, show_units=False, prec=prec, scale=scale, strip_zeros=False
129+
)
130+
spl = val_fmt.split("e")
131+
132+
# if there is an exponent, render as 'x 10^n'
133+
if eng and len(spl) > 1:
134+
num = spl[0]
135+
exp = spl[1]
136+
return f"{num} x 10^{exp}"
137+
else:
138+
return val_fmt
139+
140+
141+
# TODO: FuncFormatter for plots

0 commit comments

Comments
 (0)