summaryrefslogtreecommitdiff
path: root/libdm/libdm-report.c
diff options
context:
space:
mode:
authorZdenek Kabelac <zkabelac@redhat.com>2017-06-24 13:24:26 +0200
committerZdenek Kabelac <zkabelac@redhat.com>2017-06-24 17:44:40 +0200
commit2ef8da61eb968acb3c5c51cdfdc997e8bed90352 (patch)
tree142b7e6b56d77ce2336eb4d4974f3f13cad77d0b /libdm/libdm-report.c
parent0016b79c8b3223de7155fb4ec5fa799f7af1c811 (diff)
downloadlvm2-2ef8da61eb968acb3c5c51cdfdc997e8bed90352.tar.gz
libdm: implement dm_percent_to_round_float
Add function to adjust printing of percent values in better way. Rounding here is going along following rules: 0% & 100% are always clearly reported with .0 decimal points. Values slightly above 0% we make sure a nearest bigger non zero value with given precission is printed (i.e. 0.01 for %.2f will be shown) For values closely approaching 100% we again detect and adjust value that is less then 100 when printed. (i.e. 99.99 for %.2f will be shown). For other values we are leaving them with standard rounding mechanism since we care mainly about corner values 0 & 100 which need to be printed precisely.
Diffstat (limited to 'libdm/libdm-report.c')
-rw-r--r--libdm/libdm-report.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/libdm/libdm-report.c b/libdm/libdm-report.c
index 7caba985b..0a5d464b5 100644
--- a/libdm/libdm-report.c
+++ b/libdm/libdm-report.c
@@ -2425,6 +2425,29 @@ float dm_percent_to_float(dm_percent_t percent)
return (float) percent / DM_PERCENT_1 + 0.f;
}
+float dm_percent_to_round_float(dm_percent_t percent, unsigned digits)
+{
+ static const float power10[] = {
+ 1.f, .1f, .01f, .001f, .0001f, .00001f, .000001f,
+ .0000001f, .00000001f, .000000001f,
+ .0000000001f
+ };
+ float r;
+ float f = dm_percent_to_float(percent);
+
+ if (digits >= DM_ARRAY_SIZE(power10))
+ digits = DM_ARRAY_SIZE(power10) - 1; /* no better precision */
+
+ r = DM_PERCENT_1 * power10[digits];
+
+ if ((percent < r) && (percent > DM_PERCENT_0))
+ f = power10[digits];
+ else if ((percent > (DM_PERCENT_100 - r)) && (percent < DM_PERCENT_100))
+ f = (float) (DM_PERCENT_100 - r) / DM_PERCENT_1;
+
+ return f;
+}
+
dm_percent_t dm_make_percent(uint64_t numerator, uint64_t denominator)
{
dm_percent_t percent;