CHGame 0.3.0
Libraries for the CHGame handheld
Loading...
Searching...
No Matches
Fmt.h
Go to the documentation of this file.
1/// @file Fmt.h
2/// @brief Number formatting without printf.
3///
4/// The SDK's snprintf pulls in ~3.5 KB (and has no %l).
5#pragma once
6#include <stdint.h>
7
8/// @defgroup chgame_fmt Number formatting
9/// @ingroup lib_chgame
10/// @brief Integers, money and times as text, without printf.
11///
12/// Each call writes at p, terminates the string and returns the new end, so
13/// calls chain:
14/// @code
15/// char buf[16], *p = buf;
16/// p = fmtStr(p, "BET "); p = fmtMoney(p, bet); // "BET $25"
17/// @endcode
18/// The caller's buffer must be big enough: 12 characters hold any int32_t.
19/// @{
20
21/// @brief Write an integer: "-123".
22/// @param p Where to write.
23/// @param v The number.
24/// @return The end of the string (at its terminating 0).
25char *fmtInt(char *p, int32_t v); // "-123"
26/// @brief Write an amount of money: "$1234", "-$5".
27/// @param p Where to write.
28/// @param v Dollars.
29/// @return The end of the string.
30char *fmtMoney(char *p, int32_t v); // "$1234", "-$5"
31/// @brief Write money with thousands separators: "$1,234".
32/// @param p Where to write.
33/// @param v Dollars.
34/// @return The end of the string.
35char *fmtCash(char *p, int32_t v); // "$1,234": money with thousands separators
36/// @brief Write money short for a narrow space: as fmtMoney(), but "$12K" from $10,000.
37/// @details From 10,000 on (either sign) it writes whole thousands, rounded
38/// toward zero: 12,999 is "$12K".
39/// @param p Where to write.
40/// @param v Dollars.
41/// @return The end of the string.
42char *fmtShort(char *p, int32_t v); // as fmtMoney, but "$12K" from $10,000
43/// @brief Write a time as minutes and seconds: "1:05".
44/// @param p Where to write.
45/// @param secs Seconds.
46/// @return The end of the string.
47char *fmtTime(char *p, uint16_t secs); // "1:05"
48/// @brief Copy a string.
49/// @param p Where to write.
50/// @param s The string.
51/// @return The end of the copy.
52char *fmtStr(char *p, const char *s); // a copy of s
53
54/// @}
char * fmtShort(char *p, int32_t v)
Write money short for a narrow space: as fmtMoney(), but "$12K" from $10,000.
char * fmtStr(char *p, const char *s)
Copy a string.
char * fmtMoney(char *p, int32_t v)
Write an amount of money: "$1234", "-$5".
char * fmtInt(char *p, int32_t v)
Write an integer: "-123".
char * fmtCash(char *p, int32_t v)
Write money with thousands separators: "$1,234".
char * fmtTime(char *p, uint16_t secs)
Write a time as minutes and seconds: "1:05".