Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 19th, 2005, 08:59 PM
tarmat
Guest
 
Posts: n/a
Default std::setprecision

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
  #2  
Old July 19th, 2005, 09:00 PM
tom_usenet
Guest
 
Posts: n/a
Default 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
  #3  
Old July 19th, 2005, 09:00 PM
Fraser Ross
Guest
 
Posts: n/a
Default 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


  #4  
Old July 19th, 2005, 09:00 PM
Fraser Ross
Guest
 
Posts: n/a
Default 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]


  #5  
Old July 19th, 2005, 09:00 PM
tom_usenet
Guest
 
Posts: n/a
Default 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
  #6  
Old July 19th, 2005, 09:07 PM
Jerry Coffin
Guest
 
Posts: n/a
Default 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.
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles