473,386 Members | 1,710 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,386 software developers and data experts.

Precision Change - Float to String

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 add the
commas, I convert it to a string (specifically a char[] rather than an
actual string object) using the
gcvt function as shown below.

char amt[50];
gcvt(amount,50,amt);

The problem I run into is a change in the precision. If amount is of
type float and has a value of 264588.44 then the code above results in
the string amt being "264588.4375". Note the change in the decimal
portion of the number; I don't want this. I want the original number
in the string. People have suggested using sprintf(). However,
sprintf() won't help me since I don't know the number of decimal
places in advance of the conversion.

Is there any way around this? Keep in mind that I don't know how many
decimal places I will need.

Thanks,
Bryan
Jul 22 '05 #1
5 7960
"Bryan R. Meyer" <br***********@verizon.net> wrote...
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 add the
commas, I convert it to a string (specifically a char[] rather than an
actual string object) using the
gcvt function as shown below.

char amt[50];
gcvt(amount,50,amt);

The problem I run into is a change in the precision. If amount is of
type float and has a value of 264588.44 then the code above results in
the string amt being "264588.4375". Note the change in the decimal
portion of the number; I don't want this.
How do you know that it "has a value of 264588.44" unless you convert
it to a string (which you have) and read the result? And if such
conversion does not give you what you think it should give you, what
reason do you have to doubt it?
I want the original number
in the string.
What is "the original number"? How did it come into being? Why do
consider it "the original"? Is that a constant you wrote in the C++
source file? Do you know that many numbers cannot be represented
precisely in a double or a float? Take 0.1 for example. It cannot
be represented in a limited amount of binary digits. So, the real
number is not what you think it is, but it's kinda close. And when
time comes to convert it back into characters, some part of the
number is lost, in your case 0.0025. That's rounding and truncation
happening in memory when you store a double (or float) value in some
limited number of bits.
People have suggested using sprintf(). However,
sprintf() won't help me since I don't know the number of decimal
places in advance of the conversion.

Is there any way around this? Keep in mind that I don't know how many
decimal places I will need.


If you don't know how many you'll need, how do you know that what you're
getting is wrong?

V
Jul 22 '05 #2
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:<omgrc.89178$xw3.5049169@attbi_s04>...
What is "the original number"? How did it come into being? Why do
consider it "the original"? Is that a constant you wrote in the C++
source file? Do you know that many numbers cannot be represented
precisely in a double or a float?
I am very well aware that floating-point numbers cannot be represented
exactly. Simply for testing purposes, my "original number" is a
constant in my program. I wanted to convert the number 264588.44 from
a floating-point number to a string. I attempted to use the gcvt
function to do this and it returned a string with the vaue
"264588.4375". This is unacceptable in the context of my program.

My intention is to write a function that takes a parameter of type
float and adds commas to the number in the appropriate places. This
is achieved by first converting the float to a string. It does not
suit me that after conversion to a string that I will be adding commas
to a number that was different than the original passed into the
function. I was merely trying to determine if there was some sort of
workaround for this. I do not know all of the limitations of C++.
If you don't know how many you'll need, how do you know that what you're
getting is wrong?


The example I gave was just a constant. If I have a variable num of
type float with the value 35453.3466 then I don't want to add the
commas to a number that is different, say 35453.34656576. This holds
true for any floating-point number that may be passed into the
function.

I am just a newbie in C++. I do not know every single thing the
language can and cannot do for me. I was simply trying to get some
insight into a problem I was having and wanted some input.

Thanks,
Bryan
Jul 22 '05 #3
Bryan R. Meyer wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:<omgrc.89178$xw3.5049169@attbi_s04>...
What is "the original number"? How did it come into being? Why do
consider it "the original"? Is that a constant you wrote in the C++
source file? Do you know that many numbers cannot be represented
precisely in a double or a float?
I am very well aware that floating-point numbers cannot be represented
exactly. Simply for testing purposes, my "original number" is a
constant in my program. I wanted to convert the number 264588.44 from
a floating-point number to a string. I attempted to use the gcvt
function to do this and it returned a string with the vaue
"264588.4375". This is unacceptable in the context of my program.


Maybe 264588.44 is one of those numbers that cannot be exactly
represented in a double.
My intention is to write a function that takes a parameter of type
float
A float or a double? Note that the precision of float is typically very
limited. On most systems, that precision is less than the 8 digits that
are required for 264588.44. There is most often no good reason to
prefer float over double anyway.
and adds commas to the number in the appropriate places. This
is achieved by first converting the float to a string. It does not
suit me that after conversion to a string that I will be adding commas
to a number that was different than the original passed into the
function. I was merely trying to determine if there was some sort of
workaround for this. I do not know all of the limitations of C++.


This is not a limitation of C++. It's rather a limitation of the way
floating point values are stored in computers. You claim to have
understood that, but it doesn't seem you really have. Anyway, you could
round the value to some number of digits, but I don't know how you
could do that if you don't know how many digits to round to.
If you don't know how many you'll need, how do you know that what
you're getting is wrong?


The example I gave was just a constant. If I have a variable num of
type float with the value 35453.3466 then I don't want to add the
commas to a number that is different, say 35453.34656576. This holds
true for any floating-point number that may be passed into the
function.


Well, you can't just expect precision to come back magically after it
was reduced.

Jul 22 '05 #4
"Bryan R. Meyer" wrote:

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 add the
commas, I convert it to a string (specifically a char[] rather than an
actual string object) using the
gcvt function as shown below.

char amt[50];
gcvt(amount,50,amt);

The problem I run into is a change in the precision. If amount is of
type float and has a value of 264588.44 then the code above results in
the string amt being "264588.4375". Note the change in the decimal
portion of the number; I don't want this. I want the original number
in the string. People have suggested using sprintf(). However,
sprintf() won't help me since I don't know the number of decimal
places in advance of the conversion.

Is there any way around this? Keep in mind that I don't know how many
decimal places I will need.


Short answer: no, there isn't a way around it as you describe using floating
point.

Long answer (and then some): go read the VB sub-thread.

Possible solution: move away from a floating point number and use (or create) a
fixed-point number. Once you have a fixed-point number, you won't have to
worry about loss/change of precision inherent w/ floating point, and then you
can convert to string and manipulate as you deem necessary.
Jul 22 '05 #5
Bryan R. Meyer wrote:
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 add the commas, I convert it to a string
(specifically a char[] rather than an actual string object)
using the gcvt function as shown below.

char amt[50];
gcvt(amount, 50, amt);

The problem I run into is a change in the precision.
If amount is of type float and has a value of 264588.44
then the code above results in the string amt being "264588.4375".
But you requested 50 digits and you only got 10.
What does that tell you?
Note the change in the decimal portion of the number;
You probably meant the fractional part.
I don't want this.
I want the original number in the string.
I don't think so.
I think you want the original number in amount.
People have suggested using sprintf().
However, sprintf() won't help me since I don't know
the number of decimal places in advance of the conversion.

Is there any way around this? Keep in mind that
I don't know how many decimal places I will need. cat main.cc #include <iostream>
#include <stdlib.h>

int
main(int argc, char* argv[]) {
const
double amount = 264588.437543210987654321;
const
size_t ndigit = 50;
char amt[ndigit];
std::cout << gcvt(amount, ndigit, amt)
<< " = gcvt(amount, ndigit, amt)" << std::endl;
std::cout << gcvt(amount, 8, amt)
<< " = gcvt(amount, 8, amt)" << std::endl;
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc
./main

264588.43754321098 = gcvt(amount, ndigit, amt)
264588.44 = gcvt(amount, 8, amt)

It works just fine for me.

Jul 22 '05 #6

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

Similar topics

15
by: Ladvánszky Károly | last post by:
Entering 3.4 in Python yields 3.3999999999999999. I know it is due to the fact that 3.4 can not be precisely expressed by the powers of 2. Can the float handling rules of the underlying layers be...
2
by: TheFerryman | last post by:
Is there a convenient function for changing the precision of a float? (I have a bunch of floats that are currently printed out to 9 decimal places and I'd like them to printed out at about 4)
4
by: Roger Leigh | last post by:
Hello, I'm writing a fixed-precision floating point class, based on the ideas in the example fixed_pt class in the "Practical C++ Programming" book by Steve Oualline (O' Reilly). This uses a...
2
by: Brian van den Broek | last post by:
Hi all, I guess it is more of a maths question than a programming one, but it involves use of the decimal module, so here goes: As a self-directed learning exercise I've been working on a...
16
by: BigMan | last post by:
How can I check if assignment of a float to a double (or vice versa) will result in loss of precision?
11
by: Marcus Jacobs | last post by:
Dear Group I am encountering a precision issue while converting a text string to a float using STRTOF. For example, at times a text string "736.00000" (quotation marks added) is converted to...
15
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this...
6
by: R.Biloti | last post by:
Hi folks I wrote the naive program to show up the unit roundoff (machine precision) for single and double precision: #include <stdio.h> int main (void) { double x;
6
by: Matthew | last post by:
Hi, I want to change the precision level of floating point variables and calculations which is done in php.ini. However the server I rent for my domain does not give me access to php.ini,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.