owiecc
August 23, 2023, 11:10am
1
I am trying to use pretty formatting engineering with Formatting.jl but I fail. Is it possible to automatically format numbers with SI prefixes and, ideally, be able to select precision?

E.g.:

0.01 → 10m
0.022 → 22m
0.0222 → 22.2m
0.00001 → 10μ

With Formatting.jl and autoscaling I get:

format(0.00001, autoscale = :metric) → 0.01m

Ideally I would prefer this format: [1–1000){SI prefix}{maybe unit}

1 Like

Not tested enough, but seems to work with your examples:

```
using Formatting
function formatSI(x)
1e-3<abs(x)<1 ? format(1e3*x, autoscale=:metric) * "m" :
1e-6<abs(x)<1e-3 ? format(1e6*x, autoscale=:metric) * "μ" :
1e-9<abs(x)<1e-6 ? format(1e9*x, autoscale=:metric) * "n" :
1e-12<abs(x)<1e-9 ? format(1e12*x, autoscale=:metric) * "p" :
return format(x, autoscale=:metric)
end
```

owiecc
August 24, 2023, 10:26am
3
Thanks. I was also thinking of doing something similar but I was hoping there would be a magic switch somewhere in Formatting.jl