Improve scientific notation formatting
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
import QuickDefinitionCard from '$lib/components/QuickDefinitionCard.svelte';
|
||||
import QuickConversionExample from '$lib/components/QuickConversionExample.svelte';
|
||||
import QuickConversionTable from '$lib/components/QuickConversionTable.svelte';
|
||||
import ScientificNotationValue from '$lib/components/ScientificNotationValue.svelte';
|
||||
|
||||
export let config: CalculatorDef;
|
||||
export let showTitle = true;
|
||||
@@ -147,7 +148,9 @@
|
||||
</button>
|
||||
{#if config.factor && config.type === 'standard'}
|
||||
<span class="formula-hint">
|
||||
1 {config.labels.in1} = {config.factor}{config.offset ? ` + ${config.offset}` : ''} {config.labels.in2}
|
||||
1 {config.labels.in1} =
|
||||
<ScientificNotationValue value={config.factor} />
|
||||
{config.offset ? ` + ${config.offset}` : ''} {config.labels.in2}
|
||||
</span>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<script lang="ts">
|
||||
import { solve } from '$lib/engine';
|
||||
import type { CalculatorDef } from '$lib/data/calculators';
|
||||
import ScientificNotationValue from '$lib/components/ScientificNotationValue.svelte';
|
||||
|
||||
export let config: CalculatorDef;
|
||||
|
||||
@@ -17,10 +18,6 @@
|
||||
? solve(config, 1, exampleInput.toString(), '', '')
|
||||
: null;
|
||||
$: offset = config.offset ?? 0;
|
||||
$: formulaExpression = supportsExample
|
||||
? `${exampleInput} × ${config.factor}${offset ? ` + ${offset}` : ''}`
|
||||
: '';
|
||||
|
||||
const formatExampleValue = (value: number | null): string => {
|
||||
if (value === null || Number.isNaN(value)) {
|
||||
return '—';
|
||||
@@ -50,17 +47,27 @@
|
||||
<section class="example-card">
|
||||
<h3>How to convert {config.labels.in1} to {config.labels.in2}</h3>
|
||||
<p class="example-note">
|
||||
1 {config.labels.in1} = {config.factor}{config.offset ? ` + ${config.offset}` : ''} {config.labels.in2}
|
||||
1 {config.labels.in1} =
|
||||
<ScientificNotationValue value={config.factor} />
|
||||
{config.offset ? ` + ${config.offset}` : ''} {config.labels.in2}
|
||||
</p>
|
||||
<p class="example-note">
|
||||
1 {config.labels.in2} = {formattedReverseValue} {config.labels.in1}
|
||||
1 {config.labels.in2} =
|
||||
<ScientificNotationValue value={formattedReverseValue} fallback="—" />
|
||||
{config.labels.in1}
|
||||
</p>
|
||||
<p class="example-line">
|
||||
Example: convert {exampleInput} {config.labels.in1} to {config.labels.in2}
|
||||
</p>
|
||||
<p class="example-line">
|
||||
{formulaExpression} =
|
||||
<span class="example-result">{result.val2} {config.labels.in2}</span>
|
||||
{exampleInput}
|
||||
<span class="example-operator">×</span>
|
||||
<ScientificNotationValue value={config.factor} />
|
||||
{config.offset ? ` + ${config.offset}` : ''}
|
||||
=
|
||||
<span class="example-result">
|
||||
<ScientificNotationValue value={result.val2} fallback="—" /> {config.labels.in2}
|
||||
</span>
|
||||
</p>
|
||||
</section>
|
||||
{/if}
|
||||
@@ -93,6 +100,11 @@
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.example-operator {
|
||||
margin: 0 0.25rem;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.example-result {
|
||||
font-weight: 600;
|
||||
margin-left: 0.35rem;
|
||||
|
||||
@@ -1,20 +1,19 @@
|
||||
<script lang="ts">
|
||||
import { solve } from '$lib/engine';
|
||||
import { parseScientificNotation, type ScientificNotationParts } from '$lib/utils/formatScientific';
|
||||
import ScientificNotationValue from '$lib/components/ScientificNotationValue.svelte';
|
||||
import type { CalculatorDef } from '$lib/data/calculators';
|
||||
|
||||
export let config: CalculatorDef;
|
||||
|
||||
const numericSamples = [0.1, 0.5, 1, 2, 5, 10, 20, 50, 100];
|
||||
|
||||
type Row = { input: number; output: string; scientific?: ScientificNotationParts };
|
||||
type Row = { input: number; output: string };
|
||||
|
||||
const buildRow = (value: number): Row => {
|
||||
const formatted = solve(config, 1, value.toString(), '', '');
|
||||
return {
|
||||
input: value,
|
||||
output: formatted.val2 || '—',
|
||||
scientific: formatted.val2 ? parseScientificNotation(formatted.val2) ?? undefined : undefined,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -45,14 +44,7 @@
|
||||
<p class="chart-statement">
|
||||
Converting {row.input} <span class="chart-unit">{inputLabel}</span> into <span class="chart-unit">{outputLabel}</span> equals
|
||||
<span class="chart-output-value">
|
||||
{#if row.scientific}
|
||||
<span class="scientific-base">{row.scientific.base}</span>
|
||||
<span class="scientific-suffix">
|
||||
×10<sup>{row.scientific.exponent}</sup>
|
||||
</span>
|
||||
{:else}
|
||||
{row.output}
|
||||
{/if}
|
||||
<ScientificNotationValue value={row.output} fallback="—" />
|
||||
</span>
|
||||
<span class="chart-output-unit">{outputLabel}</span>.
|
||||
</p>
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
<script lang="ts">
|
||||
import { parseScientificNotation, type ScientificNotationParts } from '$lib/utils/formatScientific';
|
||||
|
||||
export let value: string | number | null | undefined = null;
|
||||
export let fallback = '—';
|
||||
export let className = '';
|
||||
|
||||
const formattedValue = value == null ? '' : `${value}`;
|
||||
const scientific: ScientificNotationParts | null = formattedValue
|
||||
? parseScientificNotation(formattedValue)
|
||||
: null;
|
||||
</script>
|
||||
|
||||
{#if scientific}
|
||||
<span class={`scientific-notation ${className}`.trim()}>
|
||||
<span class="scientific-base">{scientific.base}</span>
|
||||
<span class="scientific-suffix">
|
||||
×10<sup>{scientific.exponent}</sup>
|
||||
</span>
|
||||
</span>
|
||||
{:else}
|
||||
<span class={`scientific-plain ${className}`.trim()}>
|
||||
{formattedValue || fallback}
|
||||
</span>
|
||||
{/if}
|
||||
|
||||
<style>
|
||||
.scientific-notation,
|
||||
.scientific-plain {
|
||||
display: inline-flex;
|
||||
align-items: baseline;
|
||||
gap: 0.15rem;
|
||||
}
|
||||
|
||||
.scientific-suffix {
|
||||
display: inline-flex;
|
||||
align-items: baseline;
|
||||
gap: 0.15rem;
|
||||
font-size: 0.85em;
|
||||
}
|
||||
|
||||
.scientific-suffix sup {
|
||||
font-size: 0.75em;
|
||||
vertical-align: super;
|
||||
}
|
||||
</style>
|
||||
@@ -5,6 +5,7 @@
|
||||
import { onMount } from 'svelte';
|
||||
import type { PageData } from './$types';
|
||||
import { buildSeoMeta, canonicalUrl, SITE_NAME, SITE_URL, toJsonLd } from '$lib/seo';
|
||||
import ScientificNotationValue from '$lib/components/ScientificNotationValue.svelte';
|
||||
|
||||
export let data: PageData;
|
||||
|
||||
@@ -105,14 +106,18 @@
|
||||
<p>
|
||||
The conversion between {calc.labels.in1.toLowerCase()} and {calc.labels.in2.toLowerCase()}
|
||||
uses a fixed multiplication factor. One {calc.labels.in1.toLowerCase().replace(/s$/, '')} equals
|
||||
{calc.factor}{calc.offset ? ` plus an offset of ${calc.offset}` : ''} {calc.labels.in2.toLowerCase()}.
|
||||
<ScientificNotationValue value={calc.factor} />
|
||||
{calc.offset ? ` plus an offset of ${calc.offset}` : ''} {calc.labels.in2.toLowerCase()}.
|
||||
{#if calc.offset}
|
||||
This offset is common in temperature conversions, where scales differ not just in magnitude but also in their zero point.
|
||||
{/if}
|
||||
</p>
|
||||
<p>
|
||||
To convert, multiply the value in {calc.labels.in1.toLowerCase()} by {calc.factor}{calc.offset ? `, then add ${calc.offset}` : ''}.
|
||||
To convert in the opposite direction, {calc.offset ? `subtract ${calc.offset}, then ` : ''}divide by {calc.factor}.
|
||||
To convert, multiply the value in {calc.labels.in1.toLowerCase()} by
|
||||
<ScientificNotationValue value={calc.factor} />
|
||||
{calc.offset ? `, then add ${calc.offset}` : ''}.
|
||||
To convert in the opposite direction, {calc.offset ? `subtract ${calc.offset}, then ` : ''}divide by
|
||||
<ScientificNotationValue value={calc.factor} />.
|
||||
</p>
|
||||
{:else if calc.type === '3col' || calc.type === '3col-mul'}
|
||||
<p>
|
||||
|
||||
Reference in New Issue
Block a user