Hello,
Here are two questions on formatting values using printf():
1. How to strip trailing zeroes from float values?
printf("%f %f", 1.777, 1.2) displays "1.777000 1.200000",
while I want to have "1.777 1.2", without loosing 6-digit
precision.
2. Is it possible to pad values with other characters than
space and zero? printf(">%5s<", "abc") displays "> abc<".
I would like to have e.g. ">##abc<" or ">--abc<".
Thank you! 4 3623
Piotr B. wrote: Here are two questions on formatting values using printf():
1. How to strip trailing zeroes from float values? printf("%f %f", 1.777, 1.2) displays "1.777000 1.200000", while I want to have "1.777 1.2", without loosing 6-digit precision.
Use %g.
2. Is it possible to pad values with other characters than space and zero? printf(">%5s<", "abc") displays "> abc<". I would like to have e.g. ">##abc<" or ">--abc<".
Not for outputting strings. Create a temporary string and do
your own padding.
V
"Piotr B." wrote: 1. How to strip trailing zeroes from float values? printf("%f %f", 1.777, 1.2) displays "1.777000 1.200000", while I want to have "1.777 1.2", without loosing 6-digit precision.
printf("%.3f %.2f", 1.777, 1.2);
Read the standard. Don't cross post between c.l.c and c.l.c++;
they are different languages. Follow-ups set. c.l.c++ probably
won't use printf anyhow.
--
"I support the Red Sox and any team that beats the Yankees"
"Any baby snookums can be a Yankee fan, it takes real moral
fiber to be a Red Sox fan"
"I listened to Toronto come back from 3:0 in '42, I plan to
watch Boston come back from 3:0 in 04"
CBFalconer wrote: "Piotr B." wrote:
1. How to strip trailing zeroes from float values? printf("%f %f", 1.777, 1.2) displays "1.777000 1.200000", while I want to have "1.777 1.2", without loosing 6-digit precision.
printf("%.3f %.2f", 1.777, 1.2);
%.2f will still print one trailing 0 after 1.2, so you probably
meant
printf(%.3f %.1f", 1.777, 1.2); Read the standard. Don't cross post between c.l.c and c.l.c++; they are different languages.
They have a lot in common. Up until the new C Standard (come
in 1999, IIRC) they shared the same standard library.
Follow-ups set. c.l.c++ probably won't use printf anyhow.
Just to let you know, 'printf' is just as much part of C++ as it
is of C circa 1998. Whether to use any particular part of the
Standard library is, of course, up to the programmer.
Be well!
Victor Bazarov wrote: %.2f will still print one trailing 0 after 1.2, so you probably meant printf(%.3f %.1f", 1.777, 1.2);
I'm afraid this won't work, because it cuts down precision
to 3 and 1 digits. In my original post, I wrote: I want to have "1.777 1.2", without loosing 6-digit precision.
But Victor has already solved this one - I should use %g. Thanks once more.
CBFalconer wrote: won't use printf anyhow.
printf() and scanf() work about 5 times faster than cin >> and cout <<
(MingGW g++ 3.2.3 on Windows 2000/AMD Athlon XP). This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Pontus F |
last post by:
Hi I am learning C++ and I'm still trying to get a grip of pointers
and other C/C++ concepts. I would appreciate if somebody could explain
what's wrong with this code:
---begin code block---
...
|
by: Steven T. Hatton |
last post by:
OK, I take back the nasty stuff I said about printf. At least until I see a
similarly concise way of writing this using C++ formatters. I want to
convert the following to something I can put onto...
|
by: Allan Bruce |
last post by:
Hi there,
I am dumping a lot of floats to file and wish to reduce the filesize.
This is my main loop (looped several thousand times)
fprintf(fptr, "%f %f %f ", normals, normals, normals); //...
|
by: Michal Prinke |
last post by:
Hello,
Here are two questions on formatting values using printf():
1. How to strip trailing zeroes from float values?
printf("%f %f", 1.777, 1.2) displays "1.777000 1.200000",
while I want to...
|
by: David Mathog |
last post by:
size_t varies from system to system. This occasionally leads to
coding issues such as:
(void) sprintf(msg,"length of buffer: %d",strlen(msg));
which worked fine on a 32 linux but triggered...
|
by: jchludzinski |
last post by:
I tryin' to print out a string with a variable number of blanks/spaces
preceding the string. I know that if I use:
fprintf( stdout, "%12s", string );
I get 12 blanks preceding 'string'. If I...
|
by: user |
last post by:
Hi,
I am extending python with C and trying to debug with printf. The code
below succssfully returns the string "hello" when compiled and called,
but the "can print from in here phrase" does...
|
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...
|
by: john coltrane |
last post by:
Is there way to create a formatted string in a similar that is similar to
sprintf?
The same for printing, printf?
C,D,E,F,G,N,X for currency, decimal, exponential, fixed, general, numerical,...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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...
| | |