| 27 | } |
| 28 | |
| 29 | std::string ToStringPrecisionLocale(platform::Locale const & loc, double d, int pr) |
| 30 | { |
| 31 | std::string formatBuf("%0.0f"); |
| 32 | if (pr > 0) |
| 33 | { |
| 34 | if (pr < 10) |
| 35 | formatBuf[3] = '0' + pr; // E.g. replace %0.0f with %0.1f |
| 36 | else |
| 37 | LOG(LERROR, ("Too big unsupported precision", pr)); |
| 38 | } |
| 39 | |
| 40 | char textBuf[50]; |
| 41 | int const n = std::snprintf(textBuf, sizeof(textBuf), formatBuf.c_str(), d); |
| 42 | if (n < 0 || n >= static_cast<int>(sizeof(textBuf))) |
| 43 | { |
| 44 | LOG(LERROR, ("snprintf", pr, d, "failed with", n)); |
| 45 | return std::to_string(static_cast<int64_t>(d)); |
| 46 | } |
| 47 | |
| 48 | std::string out(textBuf); |
| 49 | |
| 50 | // std::locale does not work on Android NDK, so decimal and grouping (thousands) separator |
| 51 | // shall be customized manually here. |
| 52 | |
| 53 | if (pr) |
| 54 | { |
| 55 | // Value with decimals. Set locale decimal separator. |
| 56 | if (loc.m_decimalSeparator != ".") |
| 57 | out.replace(out.size() - pr - 1, 1, loc.m_decimalSeparator); |
| 58 | } |
| 59 | else |
| 60 | { |
| 61 | // Value with no decimals. Check if it's equal or bigger than 10000 to |
| 62 | // insert the grouping (thousands) separator characters. |
| 63 | if (out.size() > 4 && !loc.m_groupingSeparator.empty()) |
| 64 | for (long pos = static_cast<long>(out.size()) - 3; pos > 0; pos -= 3) |
| 65 | out.insert(pos, loc.m_groupingSeparator); |
| 66 | } |
| 67 | |
| 68 | return out; |
| 69 | } |
| 70 | |
| 71 | std::string_view DebugPrint(Units units) |
| 72 | { |