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

zero pad a double

Hi

Does anyone know how to zero-pad a float or double? I know %02d works
for ints but it doesn't work for doubles e.g. 1.2 -01.2

Thanks

Ian
Feb 26 '07 #1
2 2613
Ian T said:
Hi

Does anyone know how to zero-pad a float or double? I know %02d works
for ints but it doesn't work for doubles e.g. 1.2 -01.2
It does if you do it right:

$ cat foo.c
#include <stdio.h>
#include <math.h>

int main(void)
{
double d = 1.2;
printf("%04.1f\n", d);
return 0;
}

$ ./foo
01.2

The figure just before the dot separator gives the total width of the
field, including the radix point and any post-radix digits.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 26 '07 #2
I see that now; total field width!

Works great now.

Thanks
Ian

Richard Heathfield wrote:
Ian T said:
>Hi

Does anyone know how to zero-pad a float or double? I know %02d works
for ints but it doesn't work for doubles e.g. 1.2 -01.2

It does if you do it right:

$ cat foo.c
#include <stdio.h>
#include <math.h>

int main(void)
{
double d = 1.2;
printf("%04.1f\n", d);
return 0;
}

$ ./foo
01.2

The figure just before the dot separator gives the total width of the
field, including the radix point and any post-radix digits.
Feb 26 '07 #3

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

Similar topics

6
by: david | last post by:
Hi, I have an application as follows: MySQL database Back-Eend linked to MS Access Front-End and ASP Web Application. I require users to enter Serial Numbers such as: 0105123567 (10...
6
by: Frank | last post by:
I spent over an hour on the Internet & MSDN looking for an answer that worked for this problem. Lots of answers, none work. It's time to post a question. Using: VB6, ADO, MS Jet 4.0 How do...
53
by: Zhiqiang Ye | last post by:
Hi, All I am reading FAQ of this group. I have a question about this: http://www.eskimo.com/~scs/C-faq/q7.31.html It says: " p = malloc(m * n); memset(p, 0, m * n); The zero fill is...
8
by: Avin Patel | last post by:
Hi, Does the Double has the facilty to define -0. If it has, "==" or System.Math.Sign() doesn't able to differentiate between -0 & 0. i.e. Double x = -0.0d Double y = 0.0d if(value ==...
33
by: gk245 | last post by:
I mean, anything that follows a 0 is automatically turned into a octal number. I want to have a integer variable that will hold numbers as integers even if they begin with a zero. for example:...
3
by: Bill Pursell | last post by:
I have a program that does most of its work traversing a bunch of lists. The lists contain a void *, and I spent some time today replacing the void *'s with a copy of the data at the end of the...
4
by: H.S. | last post by:
Hello, I am trying out a few methods with which to test of a given number is practically zero. as an example, does the following test correctly if a given number is zero within machine...
8
by: cppquester | last post by:
I am forced to work with a char* buf; I allocate memory and then construct some data types in buf (several different at different locations of buf kind of struct alike) Sometimes all data...
8
by: =?Utf-8?B?bWljaGFlbGd3ZWllcg==?= | last post by:
Hello! I was working on some code the other day, and I came across an odd discrepancy between the decimal and the double type. If I attempt to divide a decimal by zero, the framework throws an...
15
by: Peng Yu | last post by:
Hi, Suppose T is 'float' or 'double'. T x; x < 10 * std::numeric_limits<T>::epsilon(); I can use the above comparison to test if 'x' is numerically zero. But I'm wondering what should be...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.