Skip to content

How to get 'best-scaled' SI strings? #4

@cobbr2

Description

@cobbr2

I'm looking for a method that scales the SI units to the most appropriate scaling prefix. E.g., if I have "4096 KiB", I see no direct method or trick that will format this to "4.0 MiB", though after perusing the source, I do see I can get there (if I already know the scale I want) by doing "4096 KiB".unit().to_s("MiB"), or ".0000032 cm".unit().to_s("nm").

But I'd like to do it without knowing the "best" scale; I often have to print values that might be 2 B, or 2 PiB, or anywhere in between. By best, I think I mean the fewest prefixes, where the prefix(es) chosen leave the scalar at a value between 1 and the next larger scaling prefix. I suspect the conversion needs to be able to say which scaling vector to use (e.g., the 2**(10x) vector vs. the 10**(3x) vector); wouldn't want to get in a fight between a RAM vendor and a disk vendor about what GB means.

I don't see anything in the referenced IEEE Xplore article about handling this part of the problem; I can see it might be a tad challenging when dealing with something like 4.2e-6 centigram nanoparsecs / kilosecond .

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions