From 67e814f1822efb278b631b44f1fe9f37bd22bf68 Mon Sep 17 00:00:00 2001 From: Alessandro Grassi Date: Wed, 23 Mar 2016 08:42:16 +0100 Subject: [PATCH] Improve to_bytes to work with input values with a decimal point --- src/common.cc | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/common.cc b/src/common.cc index 0cfdb493..ae40ab9d 100644 --- a/src/common.cc +++ b/src/common.cc @@ -733,16 +733,16 @@ void free_stock(struct text_object *obj) void print_to_bytes(struct text_object *obj, char *p, int p_max_size) { std::vector buf(max_user_text.get(*state)); - long long bytes; + long double bytes; char unit[16]; // 16 because we can also have long names (like mega-bytes) generate_text_internal(&(buf[0]), max_user_text.get(*state), *obj->sub); - if(sscanf(&(buf[0]), "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){ - if(strncasecmp("b", unit, 1) == 0) snprintf(&(buf[0]), max_user_text.get(*state), "%lli", bytes); - else if(strncasecmp("k", unit, 1) == 0) snprintf(&(buf[0]), max_user_text.get(*state), "%lli", bytes * 1024); - else if(strncasecmp("m", unit, 1) == 0) snprintf(&(buf[0]), max_user_text.get(*state), "%lli", bytes * 1024 * 1024); - else if(strncasecmp("g", unit, 1) == 0) snprintf(&(buf[0]), max_user_text.get(*state), "%lli", bytes * 1024 * 1024 * 1024); - else if(strncasecmp("t", unit, 1) == 0) snprintf(&(buf[0]), max_user_text.get(*state), "%lli", bytes * 1024 * 1024 * 1024 * 1024); + if(sscanf(&(buf[0]), "%Lf%s", &bytes, unit) == 2 && strlen(unit) < 16){ + if(strncasecmp("b", unit, 1) == 0) snprintf(&(buf[0]), max_user_text.get(*state), "%Lf", bytes); + else if(strncasecmp("k", unit, 1) == 0) snprintf(&(buf[0]), max_user_text.get(*state), "%Lf", bytes * 1024); + else if(strncasecmp("m", unit, 1) == 0) snprintf(&(buf[0]), max_user_text.get(*state), "%Lf", bytes * 1024 * 1024); + else if(strncasecmp("g", unit, 1) == 0) snprintf(&(buf[0]), max_user_text.get(*state), "%Lf", bytes * 1024 * 1024 * 1024); + else if(strncasecmp("t", unit, 1) == 0) snprintf(&(buf[0]), max_user_text.get(*state), "%Lf", bytes * 1024 * 1024 * 1024 * 1024); } snprintf(p, p_max_size, "%s", &(buf[0])); }