Connecting Tech Pros Worldwide Help | Site Map

std::setprecision

tarmat
Guest
 
Posts: n/a
#1: Jul 19 '05
I'm trying to create a function that will turn a float into a
std::string that always shows the number to two decimal places. I have
the following but it doesn't give the desired output:


inline std::string ftos(float f)
{
std::ostringstream buffer;

buffer << std::setprecision(2) << f;

return buffer.str();
}

for example

ftos (34.22) gives "34"
ftos(3.123) gives "3.1"

how do I create a function that will simply chop of all the digits
after the second digit from the decimal point?

thanks
tom_usenet
Guest
 
Posts: n/a
#2: Jul 19 '05

re: std::setprecision


On Wed, 05 Nov 2003 20:04:40 +0530, tarmat <tarmat@btopenworld.com>
wrote:
[color=blue]
>
>inline std::string ftos(float f)
>{
> std::ostringstream buffer;
>
> buffer << std::setprecision(2) << f;[/color]

buffer << std::fixed << std::setprecision(2) << f;

Tom
Fraser Ross
Guest
 
Posts: n/a
#3: Jul 19 '05

re: std::setprecision


I don't see what happened. Isn't normal notation the default? How many
notations is there?

Fraser.

[color=blue][color=green]
> >inline std::string ftos(float f)
> >{
> > std::ostringstream buffer;
> >
> > buffer << std::setprecision(2) << f;[/color]
>
> buffer << std::fixed << std::setprecision(2) << f;
>
> Tom[/color]


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.532 / Virus Database: 326 - Release Date: 27/10/2003


Fraser Ross
Guest
 
Posts: n/a
#4: Jul 19 '05

re: std::setprecision


I see those numbers did not need a E in them. Scientific notation is the
default.

Fraser.
[color=blue]
> I don't see what happened. Isn't normal notation the default? How many
> notations is there?
>
> Fraser.
>
>[color=green][color=darkred]
> > >inline std::string ftos(float f)
> > >{
> > > std::ostringstream buffer;
> > >
> > > buffer << std::setprecision(2) << f;[/color]
> >
> > buffer << std::fixed << std::setprecision(2) << f;
> >
> > Tom[/color][/color]


tom_usenet
Guest
 
Posts: n/a
#5: Jul 19 '05

re: std::setprecision


On Wed, 5 Nov 2003 17:08:15 -0000, "Fraser Ross"
<fraserATmembers.v21.co.unitedkingdom> wrote:
[color=blue]
>I don't see what happened. Isn't normal notation the default? How many
>notations is there?[/color]

3 main ones I think: normal, scientific and fixed precision.

Tom
Jerry Coffin
Guest
 
Posts: n/a
#6: Jul 19 '05

re: std::setprecision


In article <3fa9660f@news.greennet.net>, "Fraser Ross"
<fraserATmembers.v21.co.unitedkingdom> says...[color=blue]
> I don't see what happened. Isn't normal notation the default? How many
> notations is there?[/color]

In C there are three conversions: %e, %f and %g. By default, ostreams
use the equivalent of %g, which automatically chooses between %e and %f.
For ostreams, these are equivalent to scientific and fixed respectively.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Closed Thread