Print multiplier in scientific notation

This commit is contained in:
cryptogopher 2023-11-18 19:19:28 +01:00
parent 7dc73dd66d
commit 761aad358a
2 changed files with 23 additions and 1 deletions

View File

@ -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<sup>#{exponent-1}</sup>"
end
result.html_safe
end
end

View File

@ -22,7 +22,7 @@
<%= link_to unit.symbol, edit_unit_path(unit) %>
</td>
<td><%= unit.name %></td>
<td class="number"><%= unit.multiplier.to_s('3F') unless unit.multiplier == 1 %></td>
<td class="number"><%= scientifize(unit.multiplier) unless unit.multiplier == 1 %></td>
<% if current_user.at_least(:active) %>
<td class="actions">
<%= image_button_to t(".delete_unit"), "delete-outline", unit_path(unit),