473,396 Members | 1,892 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,396 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 3330

"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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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
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...
0
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...

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.