[prev in list] [next in list] [prev in thread] [next in thread] 

List:       boost
Subject:    [boost] Boost format hex issue..
From:       Rob Kramer <robk () starhub ! net ! sg>
Date:       2010-07-14 8:48:22
Message-ID: i1ju0v$6r8$1 () dough ! gmane ! org
[Download RAW message or body]

Hi,

I'm having an issue with boost::format, in that I can't get it to format a 
value as hexadecimal if that value is of float type. For example, the little 
test program below prints:

"As int: 0018, 0x0012, as float: 0018, 0x0018"

This is as intended, I suppose, since floats can't printed in hex form 
anyhow. But is there anyway I can get the 'x' format type attribute to 
interpret the value as integer? Simply casting it to int is not an option, 
because the way my float value is interpreted should depend only on the 
format string.

I added a hack to my application that fixes it for now, but that doesn't 
seem very correct:

  boost::format (..)  fmt;
  double   v = ..;

  if (fmod (v, 1.0) == 0.0)
      fmt % (int) v;

  else
      fmt % v;


Cheers!

     Rob



#include <iostream>
#include <iomanip>
#include <boost/format.hpp>

using namespace std;

int main()
{
    cout << "As int: "
         << boost::format ("%1$04d, 0x%1$04x") % 18
         << ", as float: "
         << boost::format ("%1$04d, 0x%1$04x") % (double) 18
         << endl;
}



_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic