From ac50e173f90e26b72568c62a36efffc7da94f091 Mon Sep 17 00:00:00 2001 From: hutou Date: Sat, 2 Jan 2021 21:38:59 +0100 Subject: [PATCH] Update fpjust method for use with strings --- shard.yml | 4 ++-- src/commons.cr | 13 ++++++++++--- src/tablo.cr | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/shard.yml b/shard.yml index 2a7bed7..0f12bc8 100644 --- a/shard.yml +++ b/shard.yml @@ -1,9 +1,9 @@ name: tablo -version: 0.9.4 +version: 0.9.5 authors: - Hubert Toullec -crystal: 0.35.1 +crystal: 0.36.1 license: MIT diff --git a/src/commons.cr b/src/commons.cr index 4364f61..62e60af 100644 --- a/src/commons.cr +++ b/src/commons.cr @@ -160,14 +160,21 @@ module Tablo # - nil : mere printf formatting # - 0 : non signficant zeroes removed and decimal point kept even if no more decimal digits # - 1 : non signficant zeroes removed and decimal point also removed if no more decimal digits - # - 2 : All blanks if value == zero + # - 2 : like 1, but all blanks if value == zero # - 3 : same as 0, but zero added after decimal point if no more decimal digits def fpjust(data : Array(Array(Tablo::CellType?)), col, nbdec, mode) fvleft = [] of String fvright = [] of String data.each do |r| - cell = ("%.#{nbdec}f" % r[col]) - dot = cell.index(".").as(Int32) + case r[col] + when Float64 + cell = "%.#{nbdec}f" % r[col] + else + cell = r[col].to_s + cell = "0.0" if cell == "" + cell = "#{cell}.0" if cell.index(".").nil? + end + dot = cell.as(String).index(".").as(Int32) left, right = [cell[0..dot - 1], cell[dot + 1..-1]] if !mode.nil? right = right.gsub(/0*$/, "") diff --git a/src/tablo.cr b/src/tablo.cr index 8889292..d25be64 100644 --- a/src/tablo.cr +++ b/src/tablo.cr @@ -2,6 +2,6 @@ require "./table.cr" require "./column.cr" module Tablo - VERSION = "0.9.4" + VERSION = "0.9.5" extend self end