472,954 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

formatting floating numbers into 7 digits?

Hello,

I want to format floats ( doubles ) into small strings of 7 characters.

I tried to use std::ostream, but I don't get the desired behaviour.
Especially in scientific notation I am not comfortable with things like
e+007. This is wasting 2 digits for the two zeros.

I didn't try boost::format, because it doesn't seem to me it will make
any difference on this. At least the documentation doesn't point it out.

Does anyone know a good way?

Ingo
Jul 23 '05 #1
2 3317

"Ingo Nolden" <in**********@SPAMrecurdyn.org> wrote in message
news:d6**********@svr7.m-online.net...
Hello,

I want to format floats ( doubles ) into small strings of 7 characters.
Not all required values of type 'float' or 'double'
can be represented by only 7 text characters. Do
you want those values truncated (and from which 'end'),
or are your values all within seven text characters,
or what? How many digits on either side of the decimal
do you want? IOW you don't give enough information.

I tried to use std::ostream, but I don't get the desired behaviour.
Especially in scientific notation I am not comfortable with things like
e+007. This is wasting 2 digits for the two zeros.

I didn't try boost::format, because it doesn't seem to me it will make any
difference on this. At least the documentation doesn't point it out.

Does anyone know a good way?


Once you've decided the answers to my above questions,
you can use a 'std::ostringstream' object with the manipulators
'std::fixed' and 'std::setprecision'. The former is declared
by <ios>, the latter by <iomanip>

#include <ios>
#include <iomanip>
#include <iostream>
#include <ostream>
#include <sstream>

int main()
{
double d (3.1416);
std::ostringstream oss;
oss << std::fixed << std::setprecision(2) << d;
std::cout << oss.str() << '\n'; /* prints 3.14 */
return 0;
}

Also, if you want to pad an output 'field' to a width
greater that the actual number of output characters
generated by <<, see the 'std::setw()' manipulator,
also declared by <iomanip>.

std::cout << '*' << std::setw(5) << "abc" << '*' << '\n';

/* prints *abc * */

(If the argument to 'setw()' is less than the number of
output characters generated by <<, the output width is *not*
truncated to that specified with 'setw()'.)

std::cout << std::setw(1) << "abc\n";

/* prints abc, not a */

-Mike
Jul 23 '05 #2
Mike Wahler wrote:
"Ingo Nolden" <in**********@SPAMrecurdyn.org> wrote in message
news:d6**********@svr7.m-online.net...
Hello,

I want to format floats ( doubles ) into small strings of 7 characters.

Not all required values of type 'float' or 'double'
can be represented by only 7 text characters. Do
you want those values truncated (and from which 'end'),

yes, of course and from the end that produces the least error or are your values all within seven text characters,
or what? How many digits on either side of the decimal
do you want? IOW you don't give enough information.
Ok, I'll try. I want it to print a value with the highest possible
accuracy within a maximum of 7 characters.

eg:

1234567890123344.123 -> 1.23e15
0.000000000012345678 -> 1.2-e11
1.0 -> 1.0 ( or maybe 1 or 1. )
0.123 -> 0.123

I tried to use std::ostream, but I don't get the desired behaviour.
Especially in scientific notation I am not comfortable with things like
e+007. This is wasting 2 digits for the two zeros.

I didn't try boost::format, because it doesn't seem to me it will make any
difference on this. At least the documentation doesn't point it out.

Does anyone know a good way?

Once you've decided the answers to my above questions,
you can use a 'std::ostringstream' object with the manipulators
'std::fixed' and 'std::setprecision'. The former is declared
by <ios>, the latter by <iomanip>

#include <ios>
#include <iomanip>
#include <iostream>
#include <ostream>
#include <sstream>

int main()
{
double d (3.1416);
std::ostringstream oss;
oss << std::fixed << std::setprecision(2) << d;
std::cout << oss.str() << '\n'; /* prints 3.14 */
return 0;
}

Also, if you want to pad an output 'field' to a width
greater that the actual number of output characters
generated by <<, see the 'std::setw()' manipulator,
also declared by <iomanip>.

std::cout << '*' << std::setw(5) << "abc" << '*' << '\n';

/* prints *abc * */

(If the argument to 'setw()' is less than the number of
output characters generated by <<, the output width is *not*
truncated to that specified with 'setw()'.)

std::cout << std::setw(1) << "abc\n";

/* prints abc, not a */


yes, it works well if the output is smaller than my 7 digits, but if
scientific notation is necessary to represent my number as accurate as
possible, it will use more than 7 digits, and it will also waste digits
with leading zeros in the exponent.
-Mike

I really appreciate your help

Ingo
Jul 23 '05 #3

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

Similar topics

4
by: Tommi Mäkitalo | last post by:
Hi I need to format floating-point-numbers with exact 2 digits after decimal point. I could use printf with "%.2f", but it don't use std::locale. Any ideas? -- Tommi Mäkitalo
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
1
by: Shreyas Kulkarni | last post by:
hi there, recently i have got a problem regarding calculation of sum of digits in a floating point or precision number. the weird behaviour of compiler/language is preventing me from calculating...
7
by: Hiten | last post by:
Hi please check ffollowing conditon variable float1 and float2 holds user entered value..... Answer=float1 * float2; //output must be 8.5 output must to be 8.5 but it has 8.500002, i am...
5
by: Bilgehan.Balban | last post by:
Hi, I use %#08x to print unsigned integers in hexadecimal format. According to C ref. man. Harbison & Steele, #08 stands for "pad the number with up to 8 zeroes to complete it to 8 digit...
1
by: Martin | last post by:
Hi. My question seems to be somewhat primitive, but I cannot find an appropriate solution. Is it possible to control the number of digits displayed in the exponent part of the scientific...
9
by: Odysseus | last post by:
Hello, group: I've just begun some introductory tutorials in Python. Taking off from the "word play" exercise at <http://www.greenteapress.com/thinkpython/html/book010.html#toc96> I've written...
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,...
11
by: Peter | last post by:
I have written this small app to explain an issue I'm having with a larger program. In the following code I'm taking 10 ints from the keyboard. In the call to average() these 10 ints are then...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
1
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.