473,748 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::setprecisi on

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::ostringstr eam buffer;

buffer << std::setprecisi on(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
Jul 19 '05 #1
5 22579
On Wed, 05 Nov 2003 20:04:40 +0530, tarmat <ta****@btopenw orld.com>
wrote:

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

buffer << std::setprecisi on(2) << f;


buffer << std::fixed << std::setprecisi on(2) << f;

Tom
Jul 19 '05 #2
I don't see what happened. Isn't normal notation the default? How many
notations is there?

Fraser.

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

buffer << std::setprecisi on(2) << f;


buffer << std::fixed << std::setprecisi on(2) << f;

Tom

---
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
Jul 19 '05 #3
I see those numbers did not need a E in them. Scientific notation is the
default.

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

Fraser.

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

buffer << std::setprecisi on(2) << f;


buffer << std::fixed << std::setprecisi on(2) << f;

Tom

Jul 19 '05 #4
On Wed, 5 Nov 2003 17:08:15 -0000, "Fraser Ross"
<fraserATmember s.v21.co.united kingdom> wrote:
I don't see what happened. Isn't normal notation the default? How many
notations is there?


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

Tom
Jul 19 '05 #5
In article <3f******@news. greennet.net>, "Fraser Ross"
<fraserATmember s.v21.co.united kingdom> says...
I don't see what happened. Isn't normal notation the default? How many
notations is there?


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.
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
10051
by: Woodster | last post by:
I am starting to use templates and have managed to figure out how to use std::string, std::map and make_pair successfully so far (Yeah I know - not much of a big step but I'm getting there) Previously I was using something like: char *buffer; double value = 10.5; sprintf(buffer, "text %.2f text", value);
2
8722
by: Woodster | last post by:
I am using the std::setprecision function to format variables of type double in a string however I am unsure how to stop this appearing in scientific notation. For example std::stringstream buffer; buffer << setprecision(1) << 40.0 << "° C";
5
12069
by: Mr Fish | last post by:
Is it possible for me to record floats in a std::stringstream with 100% accuracy? If so how? Here's a little prog that demonstrates how the default ss loses accuracy //----------------------------------------------------------------------------- #include <sstream> #include <iostream> using namespace std;
0
848
by: Chris Stankevitz | last post by:
I use Microsoft Visual Studio C++ 2003 .net 7.1.3088 I have a very large "solution" (1 million lines of code) with about 60 "projects". I use fast solution build to prevent slow "up-to-date" checks and get incremental linking. I occasionally add the following debug code to a particular file: #include <iostream> #include <iomanip>
1
2674
by: Gary Wessle | last post by:
hi the code below is giving me what I want but it is very ugly. and will not work for a long list of different length numbers. could you please look at it and comment. thank you the desired output is: 0.555504 of an inch foreach 12.55
3
2498
by: Anjo Gasa | last post by:
I'm having some cases where setprecision in combination with iostreams gives some unepected behavior. Consider the following program: #include <iostream> #include <iomanip> int _tmain(int argc, _TCHAR* argv) { float value = 0.063397526741027832;
7
12504
by: jacek.dziedzic | last post by:
Hello! Can someone tell me what the expected output of the following program is? #include <fstream> #include <iomanip> using namespace std;
3
5064
by: PengYu.UT | last post by:
Hi, I setprecision to be 100 for both cases. I'm wondering why the number of digits are different. Also, for a double number, I think any digits that are longer than 15 (or 16) are not meaningful, because it exceed the double number's precision limit. Even if I setprecision to be 100, shall it truncate the number to be of 15(or 16) digits?
2
5520
by: victoryusami | last post by:
I'm not 100% sure whats even going on, but I'm working on a C++ assignment for school, and its a road trip program #include <iostream> #include <iomanip> using namespace std; char const METRIC = 'M'; // Input read as metric char const ENGLISH = 'E'; // Input read as english char const QUIT = 'Q'; // Input read to quit program char const QUIT2 = 'q'; // Input read to quit program
0
8983
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8822
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9310
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9236
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6072
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4592
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3298
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.