Trimming trailing zeroes for non-rug numbers

This commit is contained in:
PaddiM8 2021-02-22 20:45:58 +01:00
parent 4334c40c3e
commit 4fe8f8b77c
2 changed files with 66 additions and 59 deletions

View File

@ -51,7 +51,10 @@ impl KalkNum {
#[wasm_bindgen(js_name = toString)] #[wasm_bindgen(js_name = toString)]
pub fn to_string(&self) -> String { pub fn to_string(&self) -> String {
self.value.to_string() self.value
.to_string()
.trim_end_matches('0')
.trim_end_matches('.')
} }
#[wasm_bindgen(js_name = toStringBig)] #[wasm_bindgen(js_name = toStringBig)]

View File

@ -1,3 +1,5 @@
<svelte:options tag="kalk-calculator" />
<script lang="ts"> <script lang="ts">
import { afterUpdate } from "svelte"; import { afterUpdate } from "svelte";
import type { Context } from "@paddim8/kalk"; import type { Context } from "@paddim8/kalk";
@ -57,7 +59,7 @@
try { try {
if (!kalkContext) kalkContext = new kalk.Context(); if (!kalkContext) kalkContext = new kalk.Context();
const result = kalkContext.evaluate(input) ?? ""; const result = kalkContext.evaluate(input) ?? "";
if (result) { if (result && result.getValue() != 0) {
const sciNot = result.toScientificNotation(); const sciNot = result.toScientificNotation();
if (sciNot.exponent > 7 || sciNot.exponent < -6) { if (sciNot.exponent > 7 || sciNot.exponent < -6) {
return [sciNot.toString(), true]; return [sciNot.toString(), true];
@ -315,6 +317,65 @@
} }
</script> </script>
<div class="calculator" bind:this={calculatorElement}>
<section class="output" bind:this={outputElement}>
<slot />
{#each outputLines as line}
<console-line byuser={line[1]}>
{#if line[1]}
<span style="color: {promptcolor}">&gt;&gt;</span>
{/if}
{@html line[0]}
</console-line>
{/each}
</section>
<section class="input-area">
<span class="prompt" style="color: {promptcolor}">&gt;&gt;&nbsp;</span>
{#await import("@paddim8/kalk")}
<span>Loading...</span>
{:then kalk}
<div
type="text"
contenteditable="true"
class="input"
placeholder={hinttext}
autocomplete="off"
autocorrect="off"
autocapitalize="off"
spellcheck="false"
use:focus
bind:this={inputElement}
on:keydown={(event) => handleKeyDown(event, kalk)}
on:keyup={handleKeyUp}
on:input={handleInput}
role="textbox"
/>
{:catch error}
<span style="color: {errorcolor}">{error}</span>
{/await}
</section>
{#if buttonpanel}
<section class="button-panel">
<button
class="arrow left"
on:click={(e) => handleArrowClick(e, true)}>←</button
>
{#each buttonRowValues as value}
<button on:click={handleButtonClick}>{value}</button>
{/each}
<button
class="arrow right"
on:click={(e) => handleArrowClick(e, false)}>→</button
>
{#if numberrow}
{#each numberRowValues as value}
<button on:click={handleButtonClick}>{value}</button>
{/each}
{/if}
</section>
{/if}
</div>
<style lang="scss"> <style lang="scss">
.calculator { .calculator {
display: flex; display: flex;
@ -429,60 +490,3 @@
} }
} }
</style> </style>
<svelte:options tag="kalk-calculator" />
<div class="calculator" bind:this={calculatorElement}>
<section class="output" bind:this={outputElement}>
<slot />
{#each outputLines as line}
<console-line byuser={line[1]}>
{#if line[1]}
<span style="color: {promptcolor}">&gt;&gt;</span>
{/if}
{@html line[0]}
</console-line>
{/each}
</section>
<section class="input-area">
<span class="prompt" style="color: {promptcolor}">&gt;&gt;&nbsp;</span>
{#await import('@paddim8/kalk')}
<span>Loading...</span>
{:then kalk}
<div
type="text"
contenteditable="true"
class="input"
placeholder={hinttext}
autocomplete="off"
autocorrect="off"
autocapitalize="off"
spellcheck="false"
use:focus
bind:this={inputElement}
on:keydown={(event) => handleKeyDown(event, kalk)}
on:keyup={handleKeyUp}
on:input={handleInput}
role="textbox" />
{:catch error}
<span style="color: {errorcolor}">{error}</span>
{/await}
</section>
{#if buttonpanel}
<section class="button-panel">
<button
class="arrow left"
on:click={(e) => handleArrowClick(e, true)}>←</button>
{#each buttonRowValues as value}
<button on:click={handleButtonClick}>{value}</button>
{/each}
<button
class="arrow right"
on:click={(e) => handleArrowClick(e, false)}>→</button>
{#if numberrow}
{#each numberRowValues as value}
<button on:click={handleButtonClick}>{value}</button>
{/each}
{/if}
</section>
{/if}
</div>