473,378 Members | 1,454 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Float to String conversion

Max
I did some searching on the subject, but still can't find the answer, so
thought I'd ask. I have a float that needs to be converted into a string
type. That float is always >= 0.00 and must have 2 numbers after the
decimal, even if those numbers are 00. What's the best way to go about this?
Jul 22 '05 #1
3 2633
Max wrote:
I did some searching on the subject, but still can't find the answer, so
thought I'd ask. I have a float that needs to be converted into a string
type. That float is always >= 0.00 and must have 2 numbers after the
decimal, even if those numbers are 00. What's the best way to go about
this?


std::ostringstream

Jul 22 '05 #2

"red floyd" <no*****@here.dude> wrote in message
news:yx*****************@newssvr27.news.prodigy.co m...
Max wrote:
I did some searching on the subject, but still can't find the answer, so
thought I'd ask. I have a float that needs to be converted into a string
type. That float is always >= 0.00 and must have 2 numbers after the
decimal, even if those numbers are 00. What's the best way to go about
this?


std::ostringstream


std::ostringstream buf;
buf << std::fixed << std::setprecision(2) << my_float;
std::string my_string = buf.str();

(untested)

john

PS using floating point values to represent money is a design error, money
is not a continuously varying quantity, it comes in discrete units called
cents. Therefore use integers for money but count in cents (or pennies or
whatever).
Jul 22 '05 #3
100% "John Harrison" <jo*************@hotmail.com>!
PS using floating point values to represent money is a design error,
money is not a continuously varying quantity, it comes in discrete
units called cents. Therefore use integers for money but count in
cents (or pennies or whatever).


Banks frequently count fractions of a cent. See the film "Office Space" for
further details :)

Also, not everyone in the world uses US$ as their currency.

Chris
--
Chris Whitworth | GT/AC Name: parm
http://home.parm.net | AC Town: Markham
Jul 22 '05 #4

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

Similar topics

5
by: Bryan R. Meyer | last post by:
I am a relatively new C++ programmer and am attempting to write a function that takes a number of type float and adds commas to it in the appropriate places. In order to manipulate the number to...
14
by: Glen Able | last post by:
Should it be possible to create a custom class, 'Float', which would behave as a drop-in replacement for the builtin float type? As mentioned in another thread, I once tried this in rather a...
7
by: Mike | last post by:
I am trying to calculate a 32bit float value from 4 int values. I sucessfully calcluated a 32bit long value: LONG l32BitData = pData; l32BitData <<= 8; l32BitData |= (byte)pData;...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
16
by: homa | last post by:
Hi! How do I do to make t-sql not rounding the result that i returned? For example: 0.9616458*60 = 57,698748 (in any calculator) while following: -------------------------------- declare...
34
by: yaniv.dg | last post by:
hi all, i building an equition with float type now,what function should i use in order to identify is someone enter a char value by mistake?
5
by: Thorsten | last post by:
Hi everyone, I am rather new to C# and have a problem that will probably seem trivial to most of you... but I hope you can still help me nevertheless.. Via the comport, I read the result of a...
10
by: Hank Stalica | last post by:
I'm having this weird problem where my code does the following conversion from string to float: 27000000.0 -27000000.00 2973999.99 -29740000.00 2989999.13 -2989999.25 The number on the left...
1
by: malick | last post by:
Hello it developers, I'm using PHPExcelReader which I downloaded from sourceforge (http://sourceforge.net/projects/phpexcelreader) To read excel xls files in php. In the file reader.php (...
14
by: Jim Langston | last post by:
The output of the following program is: 1.#INF 1 But: 1.#INF 1.#INF was expected and desired. How can I read a value of infinity from a stream?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.