From 36685aed7ff85cd78c90dde2362881b786f4a97e Mon Sep 17 00:00:00 2001 From: Eric Schubert Date: Mon, 24 Nov 2025 14:36:19 +0100 Subject: [PATCH] [#68223] improve formatting and add test cases --- app/helpers/number_format_helper.rb | 31 +++++++++++++---------- spec/helpers/number_format_helper_spec.rb | 31 ++++++++++++++++++----- 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/app/helpers/number_format_helper.rb b/app/helpers/number_format_helper.rb index 00fe4ba8221..57e4e06224d 100644 --- a/app/helpers/number_format_helper.rb +++ b/app/helpers/number_format_helper.rb @@ -30,16 +30,10 @@ module NumberFormatHelper def number_with_limit(number, opts = {}) - init_formatting_options(opts) => { - delimiter:, - separator:, - digits:, - precision:, - length_limit: - } + init_formatting_options(opts) => { digits:, precision:, length_limit: } string_number = number_with_precision(number, precision:, strip_insignificant_zeros: true) - length = string_number.delete("#{delimiter}#{separator}").length + length = string_number.delete("#{number_delimiter}#{number_separator}").length scientific_notation_needed = length > length_limit || integer_part_size(number) > digits || @@ -57,18 +51,29 @@ module NumberFormatHelper def integer_part_size(number) = number.round.to_s.size def format_scientific_notation(number, precision) - exponent = number.to_d.exponent - rounding_position = exponent - precision - number.round(-1 * rounding_position).to_d.to_s("E") + str = "%0.*e" % [precision, number] + + mantissa, exponent = str.split("e") + + # normalize mantissa + mantissa = mantissa.sub(/(\.\d*?)0+$/, '\1') + mantissa = "#{mantissa}0" if mantissa.end_with?(".") + + # normalize exponent + exp_int = exponent.to_i + + "#{mantissa}e#{exp_int}" end def init_formatting_options(opts) { - delimiter: I18n.t("number.format.delimiter"), - separator: I18n.t("number.format.separator"), digits: opts[:digits] || 7, precision: opts[:precision] || 4, length_limit: opts[:length_limit] || 9 } end + + def number_delimiter = I18n.t("number.format.delimiter") + + def number_separator = I18n.t("number.format.separator") end diff --git a/spec/helpers/number_format_helper_spec.rb b/spec/helpers/number_format_helper_spec.rb index 9d5fde729dd..45cf7acbcc6 100644 --- a/spec/helpers/number_format_helper_spec.rb +++ b/spec/helpers/number_format_helper_spec.rb @@ -33,15 +33,34 @@ require "spec_helper" RSpec.describe NumberFormatHelper do describe "#number_with_limit" do [ + # Simple cases { input: 10, opts: {}, result: "10" }, { input: 10.0, opts: {}, result: "10" }, - { input: 17203230.09932, opts: { length_limit: 8, digits: 8, precision: 3 }, result: "0.172e8" }, - { input: 17203230.09932, opts: { length_limit: 20, digits: 6, precision: 4 }, result: "0.172e8" }, - { input: 17203230.09932, opts: { length_limit: 20, digits: 6, precision: 5 }, result: "0.17203e8" }, - { input: 17203230.09932, opts: { length_limit: 20, digits: 10, precision: 3 }, result: "17203230.099" }, - { input: 8.402e-13, opts: { length_limit: 9, digits: 6, precision: 2 }, result: "0.84e-12" }, + # Defaults are converting very big and very small numbers to scientific notation + { input: 1_000_000, opts: {}, result: "1000000" }, + { input: 10_000_000, opts: {}, result: "1.0e7" }, + { input: 0.0001, opts: {}, result: "0.0001" }, + { input: 0.00001, opts: {}, result: "1.0e-5" }, + { input: 100_000.1, opts: {}, result: "100000.1" }, + { input: 100_000.0001, opts: {}, result: "1.0e5" }, + { input: 100_000.00001, opts: {}, result: "100000" }, + # precision influences scientific notation mantissa, trailing zeros are removed + { input: 17_203_230.09932, opts: { length_limit: 8, digits: 8, precision: 3 }, result: "1.72e7" }, + { input: 17_203_230.09932, opts: { length_limit: 8, digits: 8, precision: 4 }, result: "1.7203e7" }, + { input: 17_203_230.09932, opts: { length_limit: 8, digits: 8, precision: 5 }, result: "1.72032e7" }, + # If the length limit is violated AFTER precision is rounded, use scientific notation + { input: 17_203_230.09932, opts: { length_limit: 11, digits: 8, precision: 3 }, result: "17203230.099" }, + { input: 17_203_230.09932, opts: { length_limit: 10, digits: 8, precision: 3 }, result: "1.72e7" }, + # If digit limit before separator is violated, use scientific notation + { input: 17_203_230.09932, opts: { length_limit: 20, digits: 8, precision: 3 }, result: "17203230.099" }, + { input: 17_203_230.09932, opts: { length_limit: 20, digits: 7, precision: 3 }, result: "1.72e7" }, + # Very small numbers (negative exponent) are rendered correctly + { input: 7.04e-8, opts: { length_limit: 9, digits: 6, precision: 2 }, result: "7.04e-8" }, + { input: 8.402e-13, opts: { length_limit: 9, digits: 6, precision: 2 }, result: "8.4e-13" }, + # If precision is so small, the number would round to 0, show scientific notation independently of + # not violating the length limit. { input: 8.402e-13, opts: { length_limit: 20, digits: 6, precision: 20 }, result: "0.0000000000008402" }, - { input: 8.402e-13, opts: { length_limit: 9, digits: 6, precision: 4 }, result: "0.8402e-12" } + { input: 8.402e-13, opts: { length_limit: 20, digits: 6, precision: 4 }, result: "8.402e-13" } ].each do |test_case| test_case => { input:, opts:, result: }