fmtscale.c revision 3f54fd611f536639ec30dd53c48e5ec1897cc7d9
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1985-2011 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* David Korn <dgk@research.att.com> *
* Phong Vo <kpv@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
/*
* Glenn Fowler
* AT&T Research
*
* return number n scaled to metric multiples of k { 1000 1024 }
* return string length is at most 5 chars + terminating nul
*/
#include <ast.h>
#include <lclib.h>
char*
{
register Sfulong_t m;
int r;
int z;
const char* u;
char suf[3];
char* s;
char* buf;
static const char scale[] = "bkMGTPE";
u = scale;
if (n < 1000)
r = 0;
else
{
m = 0;
while (n >= k && *(u + 1))
{
m = n;
n /= k;
u++;
}
if ((r = (10 * (m % k) + (k / 2)) / k) > 9)
{
r = 0;
n++;
}
if (k == 1024 && n >= 1000)
{
n = 1;
r = 0;
u++;
}
}
s = suf;
if (u > scale)
{
if (k == 1024)
{
*s++ = *u == 'k' ? 'K' : *u;
*s++ = 'i';
}
else
*s++ = *u;
}
*s = 0;
if (n > 0 && n < 10)
else
{
if (r >= 5)
n++;
}
return buf;
}