Boost logo

Boost Users :

Subject: Re: [Boost-users] converting numbers to strings and padding the string if the number is within a specific range
From: Eugene Wee (crystalrecursion_at_[hidden])
Date: 2009-04-12 12:03:29


Hi,
On Sun, Apr 12, 2009 at 11:45 PM, Sean Farrow
<sean.farrow_at_[hidden]> wrote:
> Hi all:
> I am in the process of writing a module for an application which requires me
> to convert a number to a string, and pad the string with a leading zero (0)
> if the number is within the range one to nine or in one case, one to 100.

I wonder if just using the standard library would be enough for your
purposes, e.g.,
std::stringstream ss;
ss << std::setfill('0') << std::setw(2) << num;
// now use ss.str() for the string

It does not quite fit your stated requirements though, e.g., setw(3)
will not pad 100 to "0100", and setw(2) will pad 0 to "00", but maybe
that is just inaccuracy of wording.

Regards,
Eugene Wee


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net