* Convert a numeric value value to its "natural" string representation. * in SI units * * E.g. a value of 3000000, with units set to "W", would be converted * to "3 MW", 20000 to "20 kW", 31500 would become "31.5 kW". * * @param x The value to convert. * @param unit The unit to append to the string, or NULL if the string * has no units. * * @return A malloc()ed string represe
| 57 | * memory. |
| 58 | */ |
| 59 | SR_API char *sr_si_string_u64(uint64_t x, const char *unit) |
| 60 | { |
| 61 | if (unit == NULL) |
| 62 | unit = ""; |
| 63 | |
| 64 | if ((x >= SR_GHZ(1)) && (x % SR_GHZ(1) == 0)) { |
| 65 | return g_strdup_printf("%llu G%s", (u64_t)(x / SR_GHZ(1)), unit); |
| 66 | } else if ((x >= SR_GHZ(1)) && (x % SR_GHZ(1) != 0)) { |
| 67 | return g_strdup_printf("%llu.%llu G%s", |
| 68 | (u64_t)(x / SR_GHZ(1)), (u64_t)(x % SR_GHZ(1)), unit); |
| 69 | } else if ((x >= SR_MHZ(1)) && (x % SR_MHZ(1) == 0)) { |
| 70 | return g_strdup_printf("%llu M%s", |
| 71 | (u64_t)(x / SR_MHZ(1)), unit); |
| 72 | } else if ((x >= SR_MHZ(1)) && (x % SR_MHZ(1) != 0)) { |
| 73 | return g_strdup_printf("%llu.%llu M%s", |
| 74 | (u64_t)(x / SR_MHZ(1)), (u64_t)(x % SR_MHZ(1)), unit); |
| 75 | } else if ((x >= SR_KHZ(1)) && (x % SR_KHZ(1) == 0)) { |
| 76 | return g_strdup_printf("%llu k%s", |
| 77 | (u64_t)(x / SR_KHZ(1)), unit); |
| 78 | } else if ((x >= SR_KHZ(1)) && (x % SR_KHZ(1) != 0)) { |
| 79 | return g_strdup_printf("%llu.%llu K%s", |
| 80 | (u64_t)(x / SR_KHZ(1)), (u64_t)(x % SR_KHZ(1)), unit); |
| 81 | } else { |
| 82 | return g_strdup_printf("%llu %s", (u64_t)x, unit); |
| 83 | } |
| 84 | |
| 85 | sr_err("%s: Error creating SI units string.", __func__); |
| 86 | return NULL; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Convert a numeric value value to its "natural" string representation. |
no outgoing calls
no test coverage detected