--- C:/Users/Paul/AppData/Local/Temp/io.hpp-revBASE.svn002.tmp.hpp Thu Mar 31 19:28:33 2011 +++ I:/boost-trunk/boost/units/io.hpp Wed Aug 22 17:25:54 2012 @@ -1041,22 +1041,34 @@ template inline std::basic_ostream& operator<<(std::basic_ostream& os, const quantity& q) { + std::ostringstream oss; + // if os.width() > 0 then need to build a single string of the quantity + // with the width specified using ostringstream + // (otherwise, outputing value and unit (perhaps autoprefixed to km) + // severally would overflow the width). + // else output directly to ostream os. + if (units::get_autoprefix(os) == autoprefix_none) { - os << q.value() << ' ' << Unit(); + ((os.width()> 0) ? oss : os) << q.value() << ' ' << Unit(); } else if (units::get_autoprefix(os) == autoprefix_engineering) { - detail::maybe_print_prefixed(os, q, detail::test_norm(autoprefix_norm(q.value()))); + detail::maybe_print_prefixed(((os.width() > 0) ? oss : os), q, detail::test_norm(autoprefix_norm(q.value()))); } else if (units::get_autoprefix(os) == autoprefix_binary) { - detail::maybe_print_prefixed(os, q, detail::test_norm(autoprefix_norm(q.value()))); + detail::maybe_print_prefixed(((os.width() > 0) ? oss : os), q, detail::test_norm(autoprefix_norm(q.value()))); } else { assert(!"Autoprefixing must be one of: no_prefix, engineering_prefix, binary_prefix"); } + if (os.width() > 0) + { + os << oss.str(); // Would be "" string if oss not used. + } + return(os); }