Using units prefix dynamically to get the appropriate prefix for the size of the value?

I'd like to use the units library prefix (k, M, G ...) system, but I'd really like to determine the prefix dynamically (automatically) depending on the value. For example, if I have a length of 12300 m I'd like an output of "12.3 km" but if my length is 0.0123 m, I'd like an output of "12.3 mm". I can't see how to do this (and including the correct length unit of m or meter of course). Suggestions? Paul --- Paul A. Bristow Prizet Farmhouse Kendal, UK LA8 8AB +44 1539 561830, mobile +44 7714330204 pbristow@hetp.u-net.com

AMDG Paul A. Bristow wrote:
I'd like to use the units library prefix (k, M, G ...) system, but I'd really like to determine the prefix dynamically (automatically) depending on the value.
For example, if I have a length of 12300 m I'd like an output of "12.3 km" but if my length is 0.0123 m, I'd like an output of "12.3 mm".
I can't see how to do this (and including the correct length unit of m or meter of course).
Suggestions?
There is no way to do this right now. Currently the output operator completely separates printing the value from printing the unit. Feel free to add a feature request in trac. I can't think of a good name for a stream manipulator off hand. In Christ, Steven Watanabe

-----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Steven Watanabe Sent: Monday, November 23, 2009 2:57 PM To: boost@lists.boost.org Subject: Re: [boost] Using units prefix dynamically to get the appropriate prefix for the size of the value?
AMDG
Paul A. Bristow wrote:
I'd like to use the units library prefix (k, M, G ...) system, but I'd really like to determine the prefix dynamically (automatically) depending on the value.
For example, if I have a length of 12300 m I'd like an output of "12.3 km" but if my length is 0.0123 m, I'd like an output of "12.3 mm".
I can't see how to do this (and including the correct length unit of m or meter of course).
Suggestions?
There is no way to do this right now. Currently the output operator completely separates printing the value from printing the unit.
Feel free to add a feature request in trac.
Ticket #3660 (new Feature Requests)
I can't think of a good name for a stream manipulator off hand.
The least problem? ;-) Thanks Paul PS If you think this might be 'an exercise for the student' - give me some hints on how you think it might be done, and I'll have a go at it.

AMDG Paul A. Bristow wrote:
Feel free to add a feature request in trac.
Ticket #3660 (new Feature Requests)
I can't think of a good name for a stream manipulator off hand.
The least problem? ;-)
But possibly the most important, since it's part of the interface, while most of the rest is implementation details.
PS If you think this might be 'an exercise for the student' - give me some hints on how you think it might be done, and I'll have a go at it.
Basically it would have to work something like: if there is a top level prefix in the unit strip it off and multiply the value by the appropriate constant the prefix corresponds to log(value)/log(10) rounded down to the nearest multiple of three. Look it up in some kind of table. if it's way to big or small fall back on the default formatting with an exponent. The biggest hurdle is probably dynamic lookup of prefixes. In Christ, Steven Watanabe
participants (2)
-
Paul A. Bristow
-
Steven Watanabe