📄 ftoprtf.h
字号:
/**********************************************
The entire number is mantissa. We'll
need a zero before the decimal point
and possibly zeroes after it. Note that
"lsig" must remain zero. Format of this
number is
0 . 000.. XXX...
lzero (point) rzero rsig
**********************************************/
lzero = 1;
rzero = -decpt;
rsig = sigdigs;
}
else if( decpt >= sigdigs )
{
/********************************************
The number has no mantissa. We may
need zeroes before the decimal point.
"rzero" and "rsig" must remain zero.
Format of this number is
XXX... 000... .
lsig lzero (point)
********************************************/
lsig = sigdigs;
lzero = decpt - sigdigs;
}
else
{
/********************************************
The number straddles the decimal point.
"lzero" and "rzero" must remain zero.
Format of this number is
XXX... . XXX...
lsig (point) rsig
********************************************/
lsig = decpt;
rsig = sigdigs - decpt;
}
}
else
{
/********************************************
"e" format always has one digit in front
of the decimal point, and the decimal
point is always surrounded by significant
digits (if any exist). Format of this
number is either
X . XXX...
lsig=1 (point) rsig
or
0
lzero=1
********************************************/
if( sigdigs ) { lsig = 1; rsig = sigdigs - 1; }
else lzero = 1;
}
/************************************************************
In all formats except ("g" without alternate mode set),
the mantissa might be too small for the specified pre-
cision, and thus zeroes may have to be added to the
mantissa by setting "rprcz".
************************************************************/
if( fmt == 'g' )
{
if( lp.alternate )
{
/*****************************************************
Significant digits are counted starting with
the first digit represented by either "lsig"
or "rsig", whichever is furthest left. Note that
if "lsig" is set then one of "lzero" or "rsig"
might be set (but not both) and "rzero" will cer-
tainly be zero, so we don't have to count it.
If no significant digit exists, we start with
the one zero represented by "lzero" (which is
already set by this time).
*****************************************************/
register int len;
if( lsig ) len = lsig + lzero + rsig;
else if( rsig ) len = rsig;
else len = 1;
if( lp.precision > len ) rprcz = lp.precision - len;
}
}
else
{
/*****************************************************
Simply count the stuff on the right side of
the decimal point.
*****************************************************/
register int rlen = rzero + rsig;
if( lp.precision > rlen ) rprcz = lp.precision - rlen;
}
/************************************************************
We now have final values for rzero and rsig. Determine
"prtpt". Alternate form always requires a point.
************************************************************/
if( lp.alternate ) prtpt = 1;
else
{
/*************************************
A point will be required if any
mantissa digits will be printed.
*************************************/
prtpt = (rzero + rsig + rprcz) > 0 ? 1: 0;
}
/************************************************************
Determine the value of "expwid" and format the exponent
and if appropriate.
************************************************************/
if( !basicF )
{
register int c; /* holds emitted characters */
register int magnitude; /* magnitude of printed number */
/**************************************************
Set things up according to exponent sign.
**************************************************/
if( expval < 0 ) { c = '-'; magnitude = -expval; }
else { c = '+'; magnitude = expval; }
/**************************************************
Format!
**************************************************/
expp = ebuf+EBSZ; /* exponent pointer */
do { *--expp = '0' + (magnitude % 10); } while( magnitude /= 10 );
if ( expp == ebuf+EBSZ-1 ) *--expp = '0'; /* at least two digits */
*--expp = c; /* sign character */
*--expp = wasupper? 'E': 'e';
expwid = ebuf + EBSZ - expp;
}
/************************************************************
At this point we've calculated the entire CENTER,
the sign, and everything relating to the exponent.
Calculate string length so far.
************************************************************/
slength = (sign? 1: 0) + /* sign width */
lsig + lzero + /* total left side digits */
prtpt + /* decimal point */
rzero + rsig + rprcz + /* total right side digits */
expwid; /* exponent width */
/************************************************************
We've done everything except figure out how much
padding to generate, so generate padding now.
************************************************************/
if( lp.left )
{
/*******************************************
Left justified; use right pad.
*******************************************/
if( lp.width > slength ) rpadb = lp.width - slength;
}
else
{
/*******************************************
Right justified; use left pad.
*******************************************/
if( lp.width > slength )
{
register int diff = lp.width - slength;
if( lp.lpad == '0' ) lpadz = diff;
else lpadb = diff;
}
}
/************************************************************
Our number is now totally formatted. Print it.
************************************************************/
{
register int i; /* index var */
register int c; /* scratch var for chars */
register char *p = conv; /* point to significant digits */
/****************************************************
Print the PREFIX.
****************************************************/
/* left blank pad */
for( i=0; i<lpadb; ++i )
{ if( (*put)( ' ', where ) != ' ' ) return -1; }
/* sign */
if( sign ) { if( (*put)( sign, where ) != sign ) return -1; }
/* left zero pad */
for( i=0; i<lpadz; ++i )
{ if( (*put)( '0', where ) != '0' ) return -1; }
/****************************************************
Print the CENTER.
****************************************************/
/* left significant digits */
for( i=0; i<lsig; ++i )
{
c = *p++;
if( (*put)( c, where ) != c ) return -1;
}
/* left zero fill */
for( i=0; i<lzero; ++i )
{ if( (*put)( '0', where ) != '0' ) return -1; }
/* decimal point */
if( prtpt ) { if( (*put)( '.', where ) != '.' ) return -1; }
/* right zero fill */
for( i=0; i<rzero; ++i )
{ if( (*put)( '0', where ) != '0' ) return -1; }
/* right significant digits */
for( i=0; i<rsig; ++i )
{
c = *p++;
if( (*put)( c, where ) != c ) return -1;
}
/* precision padding (zeroes) */
for( i=0; i<rprcz; ++i )
{
if( (*put)( '0', where ) != '0' ) return -1;
}
/****************************************************
Print the SUFFIX.
****************************************************/
/* exponent */
p = expp;
for( i=0; i<expwid; ++i )
{
c = *p++;
if( (*put)( c, where ) != c ) return -1;
}
/* right blank pad */
for( i=0; i<rpadb; ++i )
{ if( (*put)( ' ', where ) != ' ' ) return -1; }
}
/*************************************************************
"slength" does not yet count the padding, so add
the padding length here.
*************************************************************/
return slength + lpadb + lpadz + rpadb;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -