Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9016019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:53:27+00:00 2026-06-16T03:53:27+00:00

The boost::units library provides a nice way to write quantity values to a stream,

  • 0

The boost::units library provides a nice way to write quantity values to a stream, see for example.

Can I use printf with the quantity values instead of using the output stream? I have a fair amount of code which has formatted the output nicely using printf and I would like to keep the formatting. Passing the quantity into say

quantity<mass_density> rho = 1.0 * mass_density;
printf("rho %6.2e\n", rho);

throws the warning

warning: format ‘%e’ expects argument of type ‘double’, but argument 2
has type
‘boost::units::quantity<boost::units::unit<boost::units::list<boost::units::dim<boost::units::length_base_dimension,
boost::units::static_rational<-0x00000000000000003l> >,
boost::units::list<boost::units::dim<boost::units::mass_base_dimension,
boost::units::static_rational<1l> >,
boost::units::dimensionless_type> >,
boost::units::homogeneous_system<boost::units::list<boost::units::si::meter_base_unit,
boost::units::list<boost::units::scaled_base_unit<boost::units::cgs::gram_base_unit,
boost::units::scale<10l, boost::units::static_rational<3l>
> >, boost::units::list<boost::units::si::second_base_unit,
boost::units::list<boost::units::si::ampere_base_unit,
boost::units::list<boost::units::si::kelvin_base_unit,
boost::units::list<boost::units::si::mole_base_unit,
boost::units::list<boost::units::si::candela_base_unit,
boost::units::list<boost::units::angle::radian_base_unit,
boost::units::list<boost::units::angle::steradian_base_unit,
boost::units::dimensionless_type> > > > > > >
> > > > >’ [-Wformat]

I could just use rho.value() instead, but I would like to output the units if possible (even if I have update the format string).

I think the answer is in here, http://www.boost.org/doc/libs/1_52_0/doc/html/boost_units/Reference.html#header.boost.units.io_hpp

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:53:29+00:00Added an answer on June 16, 2026 at 3:53 am

    The list of format specifiers is fixed by the language. "%e" outputs a double, "%d" outputs an int etc. There’s nothing that would output an object of type quantity<mass_density>, or any other object of a class type.

    You can have this:

    template <typename Unit, typename T>
    string symbolic_units(const quantity<Unit, T> &)
    {
        return symbol_string(Unit());
    }
    
    int main()
    {
        quantity<mass_density> x;
        x = 3.72 * kilogram_per_cubic_meter;
    
        printf ("%g %s\n", x.value(), symbolic_units(x).c_str());
    }
    

    but not much more. Unless you are willing to use streams of course.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a really minimalist example. I'm using Boost.Units in the following way: #define
Greetings all. I am writing some code using the Boost Units library and have
how can I get current time with library boost. I can do this: ptime
When writing a header library (like Boost ), can one define free-floating (non-method) functions
How can I convert the following code to use boost unit test framework: #include
I'm trying to use boost unit testing alongside the Allegro graphics library, but both
I'm learning how to use the Boost Test Library at the moment, and I
We have started using the boost unit testing library for a large existing code
I'm using the Boost::Test library for unit testing, and I've in general been hacking
I'm trying to use boost::filesystem to copy files and folders (just like a standard

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.