From 5d9c320a7e315a3fa114a1f2038cf544401aa5d6 Mon Sep 17 00:00:00 2001 From: mikupls <93015331+mikupls@users.noreply.github.com> Date: Sun, 21 Nov 2021 05:07:45 +0100 Subject: [PATCH] Format post and comment votes with a decimal place, like vanilla reddit does. (#324) * Format post and comment votes with a decimal place, like vanilla reddit does. Before this change, a vote count of 1999 was displayed as 1k, which is a pretty big gap. The displayed count also differed from what Reddit does. Now, the behaviour is consistent. Added some tests for format_num. * Provide more space for post scores Co-authored-by: spikecodes <19519553+spikecodes@users.noreply.github.com> --- src/utils.rs | 37 ++++++++++++++++++++++++++++++++++--- static/style.css | 10 +++++----- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index fdb1e1c..2c74783 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -544,12 +544,14 @@ pub fn rewrite_urls(input_text: &str) -> String { }) } -// Append `m` and `k` for millions and thousands respectively +// Format vote count to a string that will be displayed. +// Append `m` and `k` for millions and thousands respectively, and +// round to the nearest tenth. pub fn format_num(num: i64) -> (String, String) { let truncated = if num >= 1_000_000 || num <= -1_000_000 { - format!("{}m", num / 1_000_000) + format!("{:.1}m", num as f64 / 1_000_000.0) } else if num >= 1000 || num <= -1000 { - format!("{}k", num / 1_000) + format!("{:.1}k", num as f64 / 1_000.0) } else { num.to_string() }; @@ -628,3 +630,32 @@ pub async fn error(req: Request
, msg: String) -> Result