 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Philip Diegmann Guest
|
Posted: Sun Feb 06, 2005 4:28 pm Post subject: sprintf (formatierte Ausgabe) |
|
|
Hallo,
Ist es möglich die Auzahl der Nachkommastellen bei einer floating point
Ausgabe mit dem sprintf-Kommando zu begrenzen.
z.B.: sprintf(pszText, "%f", dNumber);
Gruß
Philip
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Sun Feb 06, 2005 8:08 pm Post subject: Re: sprintf (formatierte Ausgabe) |
|
|
Philip Diegmann <black.ice (AT) t-online (DOT) de> writes:
| Quote: | Ist es möglich die Auzahl der Nachkommastellen bei einer floating
point Ausgabe mit dem sprintf-Kommando zu begrenzen.
z.B.: sprintf(pszText, "%f", dNumber);
|
Mit sprintf geht das nicht, aber mit snprintf:
int snprintf(char *str, size_t size, const char *format, ...);
....
The functions snprintf
and vsnprintf do not write more than size bytes (including
the trailing ' '). If the output was truncated due to
this limit then the return value is the number of charac
ters (not including the trailing ' ') which would have
been written to the final string if enough space had been
available. Thus, a return value of size or more means that
the output was truncated. (See also below under NOTES.)
snprintf ist aber erst seit C99 im Standard, gehört also streng
genommen nicht zu Standard C++.
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
King Leo - Martin Oberzal Guest
|
Posted: Sun Feb 06, 2005 10:31 pm Post subject: Re: sprintf (formatierte Ausgabe) |
|
|
Philip Diegmann wrote:
| Quote: | Ist es möglich die Auzahl der Nachkommastellen bei einer floating point
Ausgabe mit dem sprintf-Kommando zu begrenzen.
z.B.: sprintf(pszText, "%f", dNumber);
|
#include <stdio.h>
int main()
{
printf( "%f t %.2fn", 1.0/3.0, 1.0/3.0 );
}
martin@chello212186192051:~> ./foo2
0.333333 0.33
Gruß, Martin!
--
Gebrauchtwagen => http://www.austromobil.at
Fuer Privatpersonen kostenlos!
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Dirk Jagdmann Guest
|
Posted: Sun Feb 06, 2005 10:36 pm Post subject: Re: sprintf (formatierte Ausgabe) |
|
|
| Quote: | Ist es möglich die Auzahl der Nachkommastellen bei einer floating point
Ausgabe mit dem sprintf-Kommando zu begrenzen.
|
printf("%.2f", 3,1498) := 3.15
printf("%.2f", 3.1411) := 3.14
rundet sogar.
--
---> doj / cubic
----> http://cubic.org/~doj
-----> http://llg.cubic.org
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Philip Diegmann Guest
|
Posted: Mon Feb 07, 2005 7:00 pm Post subject: Re: sprintf (formatierte Ausgabe) |
|
|
Danke für die Antworten!
Es hat geklappt.
Gruß
Philip
Dirk Jagdmann wrote:
| Quote: | Ist es möglich die Auzahl der Nachkommastellen bei einer floating
point Ausgabe mit dem sprintf-Kommando zu begrenzen.
printf("%.2f", 3,1498) := 3.15
printf("%.2f", 3.1411) := 3.14
rundet sogar.
|
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|