From ce59711d67658bf550772158087923db5a716d51 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Thu, 4 Aug 2022 03:11:47 +0100 Subject: chat --- chat.lua | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/chat.lua b/chat.lua index a34086b..a4ba800 100644 --- a/chat.lua +++ b/chat.lua @@ -35,6 +35,28 @@ local function pad_c(s,width) return string.rep(" ",hspace)..s end + +local function round(n) + -- to nearest int + local f = math.floor(n) + if n>=f+0.5 then return math.ceil(n) else return f end +end + +local function round_dp(n,dp) + local exp = 10^dp + return round(n*exp)/exp +end + +local function sci(x) + local b = math.floor(math.log10(x)) + local a = round_dp(x/10^b,2) + return a.."e"..b +end + +local function maybe_sci(x) + if #tostring(x) >= scorewidth then return sci(x) else return x end +end + -- drawing -- lb @@ -72,7 +94,7 @@ local function draw_lb(W,scores) -- score W.setTextColor(colors.red) W.setCursorPos(w-scorewidth+1,i+1) - W.write(pad_r(score,scorewidth)) + W.write(pad_r(maybe_sci(score),scorewidth)) end end -- cgit v1.2.3