From 761aad358a05f0f71828c9ffc921d42fefead7b8 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sat, 18 Nov 2023 19:19:28 +0100 Subject: [PATCH] Print multiplier in scientific notation --- app/helpers/application_helper.rb | 22 ++++++++++++++++++++++ app/views/units/index.html.erb | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index f0845a7..70f8338 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -99,4 +99,26 @@ module ApplicationHelper send "#{type}_to", name, options, html_options end + + # Converts value to HTML formatted scientific notation + def scientifize(d) + sign, coefficient, base, exponent = d.split + return 'NaN' unless sign + + result = (sign == -1 ? '-' : '') + unless coefficient == '1' && sign == 1 + if coefficient.length > 1 + result += coefficient.insert(1, '.') + elsif + result += coefficient + end + if exponent != 1 + result += "×" + end + end + if exponent != 1 + result += "10#{exponent-1}" + end + result.html_safe + end end diff --git a/app/views/units/index.html.erb b/app/views/units/index.html.erb index 98dc7cc..4511a70 100644 --- a/app/views/units/index.html.erb +++ b/app/views/units/index.html.erb @@ -22,7 +22,7 @@ <%= link_to unit.symbol, edit_unit_path(unit) %> <%= unit.name %> - <%= unit.multiplier.to_s('3F') unless unit.multiplier == 1 %> + <%= scientifize(unit.multiplier) unless unit.multiplier == 1 %> <% if current_user.at_least(:active) %> <%= image_button_to t(".delete_unit"), "delete-outline", unit_path(unit),