Skip to content

Commit 9d1097f

Browse files
Update cstring.h
1 parent 7d96028 commit 9d1097f

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

code/logic/fossil/io/cstring.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,32 @@ int fossil_io_cstring_money_to_string(double amount, cstring output, size_t size
7474
*/
7575
int fossil_io_cstring_string_to_money(ccstring input, double *amount);
7676

77+
/**
78+
* @brief Converts a double amount into a formatted money string with optional currency symbol.
79+
*
80+
* Example: 1234.56 -> "$1,234.56" (USD default)
81+
*
82+
* @param amount The numeric amount to convert.
83+
* @param output Buffer to store the formatted string.
84+
* @param size Size of the output buffer.
85+
* @param currency Currency symbol to prepend (e.g., "$", "€", "¥"); NULL defaults to "$".
86+
* @return 0 on success, -1 if the buffer is too small or invalid.
87+
*/
88+
int fossil_io_cstring_money_to_string_currency(double amount, char *output, size_t size, const char *currency);
89+
90+
/**
91+
* @brief Parses a money string into a numeric double value.
92+
*
93+
* Detects and ignores a currency symbol at the start.
94+
*
95+
* Example: "$1,234.56" -> 1234.56
96+
*
97+
* @param input Input string representing money.
98+
* @param amount Pointer to store the parsed numeric value.
99+
* @return 0 on success, -1 on failure (invalid format).
100+
*/
101+
int fossil_io_cstring_string_to_money_currency(const char *input, double *amount);
102+
77103
/**
78104
* @brief Tokenizes a string by delimiters (reentrant version).
79105
*
@@ -1131,6 +1157,36 @@ namespace fossil {
11311157
static bool string_to_money(const std::string &input, double &amount) {
11321158
return fossil_io_cstring_string_to_money(input.c_str(), &amount) == 0;
11331159
}
1160+
1161+
/**
1162+
* @brief Converts a double amount into a formatted money string with optional currency symbol.
1163+
*
1164+
* Example: 1234.56 -> "$1,234.56" (USD default)
1165+
*
1166+
* @param amount The numeric amount to convert.
1167+
* @param output Buffer to store the formatted string.
1168+
* @param size Size of the output buffer.
1169+
* @param currency Currency symbol to prepend (e.g., "$", "€", "¥"); NULL defaults to "$".
1170+
* @return 0 on success, -1 if the buffer is too small or invalid.
1171+
*/
1172+
static int to_string_currency(double amount, std::string &output, size_t size, const std::string &currency){
1173+
return fossil_io_cstring_money_to_string_currency(amount, output.c_str(), currency.c_str());
1174+
}
1175+
1176+
/**
1177+
* @brief Parses a money string into a numeric double value.
1178+
*
1179+
* Detects and ignores a currency symbol at the start.
1180+
*
1181+
* Example: "$1,234.56" -> 1234.56
1182+
*
1183+
* @param input Input string representing money.
1184+
* @param amount Pointer to store the parsed numeric value.
1185+
* @return 0 on success, -1 on failure (invalid format).
1186+
*/
1187+
static int to_money_currency(const std::string &input, double *amount) {
1188+
return fossil_io_cstring_string_to_money_currency(input.c_str(), amount);
1189+
}
11341190

11351191
/**
11361192
* Creates a copy of the given cstring.

0 commit comments

Comments
 (0)