473,729 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

printf question, leading zero in E format?

Whatever format I try in Printf, an 'E' format number nearly always
has a leading non-zero:-

1.2345E7

-9.3456E8 etc.

Is it possible to force it (printf) always to have leading zero?

Thus, the above becomes:-

0.12345E8

-0.93456E9

Jun 7 '07 #1
4 3216
bobm2005 <bo*******@post master.co.ukwri tes:
Whatever format I try in Printf, an 'E' format number nearly always
has a leading non-zero:-

1.2345E7

-9.3456E8 etc.

Is it possible to force it (printf) always to have leading zero?
No. The fprintf specification says this:

e,E A double argument representing a floating-point number is
converted in the style [-]d.ddd e±dd, where there
is one digit (which is nonzero if the argument is
nonzero) before the decimal-point character and the
number of digits after it is equal to the precision [...]

So the digit before the decimal point is always nonzero if the
number to format is nonzero.
--
Comp-sci PhD expected before end of 2007
Seeking industrial or academic position *outside California* in 2008
Jun 7 '07 #2
On Thu, 07 Jun 2007 10:36:50 -0700 (while OU was sucking), bobm2005
wrote:
>Whatever format I try in Printf, an 'E' format number nearly always
has a leading non-zero:-

1.2345E7

-9.3456E8 etc.

Is it possible to force it (printf) always to have leading zero?

Thus, the above becomes:-

0.12345E8

-0.93456E9
Someone already said no to your question.

What I was going to recommend is that you could write your own. I
wrote my own for some embedded work I was doing (standard SPRINTF is a
memory/code hog). Here is the function, it could be modified to print
the numbers between 0 and 1 instead of 1 to 10.

RonB

// value = value to be printed
// total number of spaces to be occupied
// the z in "%zE"
void outputExp(float value, char total)
{ // # of digits to be printed AFTER decimal point
#define POST_DEC 6

// leading spaces of the
while(total 12){
out_putchar(' ');
--total;
}
total = 0;

// FIRST PRINT SIGN
if((*((char*)&v alue) & 0x80) == 0x80)
out_putchar('-');
else
out_putchar(' ');
*((char*)&value ) &= 0x7F;

// ZERO CASE
if(value == 0)
out_putString(" 0.000000E+00");
else{
// NUMBER HAS A POSITVE EXPONENT
TF_3 = value;
if(value >= 10){
while(TF_3 >= 10){
TF_3 /= 10;
++total;
}
}
// NUMBER HAS A NEGATIVE EXPONENT
else if(value < 1){
while(TF_3 < 1){
TF_3 *= 10;
++total;
}
total = -total;
}

// TURN NUMBER INTO 7 DIGIT LONG
{
char i= 8-total;
TF_3 = 1;
if(i >= 10){
value *= 1e9;
i -= 9;
}
for(i; i 0; --i)
TF_3 *= 10;
value *= TF_3;
}
// PRINT CHAR BY CHAR
{
xdata long print = value + 5; // ROUNDING
xdata long div = 100000000;
while(div 10){
char ch = print / div;
out_putchar(ch + '0'); //CHAR PRINTER
if(div == 100000000)
out_putchar('.' );
print -= (div * ch);
div /= 10;
}
out_putchar('E' );
if(total < 0){
out_putchar('-');
total = -total;
}
else
out_putchar('+' );
outputInt(total , 2, 1);
// this is my own Integer printer
}
}
}
Jun 7 '07 #3
On Jun 8, 6:59 am, Ron Blancarte
void outputExp(float value, char total)
{
if((*((char*)&v alue) & 0x80) == 0x80)
out_putchar('-');
else
out_putchar(' ');
*((char*)&value ) &= 0x7F;
What possessed you to write that monstrosity
instead of:
if ( value < 0 )
{
out_putchar('-');
value = -value;
}
else
out.putchar(' ');

BTW what is the purpose of "== 0x80" ? What else
could it equal besides that and 0?

Jun 7 '07 #4
On Thu, 07 Jun 2007 13:59:06 -0500, Ron Blancarte
<ron@---TAKETHISOUT---.blancarte.comw rote:
>On Thu, 07 Jun 2007 10:36:50 -0700 (while OU was sucking), bobm2005
wrote:
>>Whatever format I try in Printf, an 'E' format number nearly always
has a leading non-zero:-

1.2345E7

-9.3456E8 etc.

Is it possible to force it (printf) always to have leading zero?

Thus, the above becomes:-

0.12345E8

-0.93456E9

Someone already said no to your question.

What I was going to recommend is that you could write your own. I
wrote my own for some embedded work I was doing (standard SPRINTF is a
memory/code hog). Here is the function, it could be modified to print
the numbers between 0 and 1 instead of 1 to 10.

RonB

// value = value to be printed
// total number of spaces to be occupied
// the z in "%zE"
void outputExp(float value, char total)
{ // # of digits to be printed AFTER decimal point
#define POST_DEC 6

// leading spaces of the
while(total 12){
out_putchar(' ');
Where is this function defined? Does it do something the standard
putchar() won't?
> --total;
}
total = 0;

// FIRST PRINT SIGN
if((*((char*)&v alue) & 0x80) == 0x80)
out_putchar('-');
else
out_putchar(' ');
*((char*)&value ) &= 0x7F;
Why make the code dependent on some particular floating point
representation? You can compare value to 0.0 and run from there.
>
// ZERO CASE
if(value == 0)
out_putString(" 0.000000E+00");
else{
// NUMBER HAS A POSITVE EXPONENT
TF_3 = value;
Where is TF_3 defined?
> if(value >= 10){
while(TF_3 >= 10){
TF_3 /= 10;
++total;
}
}
// NUMBER HAS A NEGATIVE EXPONENT
else if(value < 1){
while(TF_3 < 1){
TF_3 *= 10;
++total;
}
total = -total;
}

// TURN NUMBER INTO 7 DIGIT LONG
{
char i= 8-total;
TF_3 = 1;
if(i >= 10){
value *= 1e9;
i -= 9;
}
for(i; i 0; --i)
TF_3 *= 10;
value *= TF_3;
}
// PRINT CHAR BY CHAR
{
xdata long print = value + 5; // ROUNDING
xdata long div = 100000000;
while(div 10){
char ch = print / div;
out_putchar(ch + '0'); //CHAR PRINTER
if(div == 100000000)
out_putchar('.' );
print -= (div * ch);
div /= 10;
}
out_putchar('E' );
if(total < 0){
out_putchar('-');
total = -total;
}
else
out_putchar('+' );
outputInt(total , 2, 1);
// this is my own Integer printer
}
}
}

Remove del for email
Jun 8 '07 #5

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

Similar topics

5
20427
by: OneDay | last post by:
I've got a field that has some old data with text in it, but all forward data will be a 3 digit number. But many of the numbers are still only 2 digits. I would like to force the leading zero in the entry of the field. For example if the number 77 is entered into the field, 077 will display. How do I format to force the leading zero?
8
2182
by: Johan Lindh | last post by:
Where can I find a comprehensive test suite for printf()? It seems like most implementations go wrong at sensitive spots, like printf("#.0o",0) (which should print "0") printing nothing at all. /J
7
2553
by: sunfiresg | last post by:
During an interview, I am asked to answer a question: Printf is a major formatted output function provided by the standard C library. Printf accepts a formatting string followed by a various number of arguments to replace formatting specifiers in the formatting string. You should implement the subset of the printf function compliant to the following specification.
13
22831
by: pb | last post by:
Im wanted to pad out blank spaces with a specific character instead of spaces or zeros, does C support that? printf("$%*d", '*', 5); // Not sure what the format string is supposed to look like to do this example output i would want is this: $********5
7
96315
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %% should be used. Wouldn't it have been better (from design perspective) if the same escape character had been used in this case too. Forgive me for posting without verfying things with any standard compiler, i don't have the means for now.
5
24596
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 number". Is this correct understanding? However this is not always the case. I sometimes see 4, sometimes 2, that does not complete the whole number into 8 digits. I don't know what changes this, but do you have an alternative that definitely pads to 8...
5
3477
by: GarryJones | last post by:
I have code numbers in 2 fields from a table which correspond to month and date. (Month, Code number) Field name = ml_mna 1 2 3 etc up to 12 (Data is entered without a leading zero)
6
7760
by: JimmyKoolPantz | last post by:
Task: Customer wants a script of the data that was processed in a "CSV" file. Problem: Zip-Code leading zeros are dropped Basically we have a client that has requested a custom script for each file that he has us process. He wants this in a Comma Delimited Format.
43
366
by: Jrdman | last post by:
someone has an idea on how the printf function is programmed ?
0
8917
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9200
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8148
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.