]>
git.ipfire.org Git - thirdparty/squid.git/blob - lib/util.cc
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
38 /* somewhat safer calculation of %s */
40 xpercent(double part
, double whole
)
42 return xdiv(100 * part
, whole
);
46 xpercentInt(double part
, double whole
)
48 return (int) rint(xpercent(part
, whole
));
51 /* somewhat safer division */
53 xdiv(double nom
, double denom
)
55 return (denom
!= 0.0) ? nom
/ denom
: -1.0;
58 /* integer to string */
62 static char buf
[24]; /* 2^64 = 18446744073709551616 */
63 snprintf(buf
, sizeof(buf
), "%d", num
);
67 /* int64_t to string */
69 xint64toa(int64_t num
)
71 static char buf
[24]; /* 2^64 = 18446744073709551616 */
72 snprintf(buf
, sizeof(buf
), "%" PRId64
, num
);
77 double_to_str(char *buf
, int buf_size
, double value
)
82 snprintf(buf
, buf_size
, "%.2f MB", value
/ 1e6
);
83 else if (value
< 1e12
)
84 snprintf(buf
, buf_size
, "%.3f GB", value
/ 1e9
);
86 snprintf(buf
, buf_size
, "%.4f TB", value
/ 1e12
);