calculator

package
v0.0.20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2026 License: MIT Imports: 8 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	OpAdd = '+'
	OpSub = '−'
	OpMul = '×'
	OpDiv = '÷'
)

Operators accepted by Operator.

Variables

This section is empty.

Functions

func NewForm

func NewForm() *ui.Form

NewForm is a desktop calculator in the style of the Windows one: a large right-aligned display under the pending expression, a keypad of expanding buttons with the "=" as the accent (primary role), memory keys, full keyboard input (digits, operators, Enter, Backspace, Esc, Delete), Ctrl+C / Ctrl+V through the clipboard, a main menu, and a history Table that is shown with a ToggleSwitch or View > History; a click on a row recalls its result, a right click opens a context menu. The logic is in Engine, a plain Go type with unit tests.

func ParseNumber

func ParseNumber(text string) (float64, bool)

ParseNumber parses a number the way people write it: with spaces, thousands separators or a decimal comma.

Types

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine is the calculator logic without any UI: a standard calculator that evaluates left to right (2 + 3 × 4 = 20), repeats the last operation on a repeated "=", and has a memory register and a history of results.

func NewEngine

func NewEngine() *Engine

NewEngine returns a calculator showing 0.

func (*Engine) Backspace

func (e *Engine) Backspace()

Backspace erases the last typed character.

func (*Engine) Clear

func (e *Engine) Clear()

Clear (C) resets the calculation; memory and history are kept.

func (*Engine) ClearEntry

func (e *Engine) ClearEntry()

ClearEntry (CE) clears the current number, keeping the pending operation.

func (*Engine) ClearHistory

func (e *Engine) ClearHistory()

ClearHistory forgets the finished calculations.

func (*Engine) Digit

func (e *Engine) Digit(d int)

Digit types a digit 0-9.

func (*Engine) Display

func (e *Engine) Display() string

Display is the current number with thousands separators, or the error.

func (*Engine) Equals

func (e *Engine) Equals()

Equals evaluates the pending operation; without one it repeats the last.

func (*Engine) Expression

func (e *Engine) Expression() string

Expression is the pending expression: "12 + 7 =".

func (*Engine) HasError

func (e *Engine) HasError() bool

HasError reports whether an error is shown.

func (*Engine) HasMemory

func (e *Engine) HasMemory() bool

HasMemory reports whether the memory register holds a value.

func (*Engine) History

func (e *Engine) History() []HistoryEntry

History returns the finished calculations, the oldest first.

func (*Engine) MemoryAdd

func (e *Engine) MemoryAdd()

MemoryAdd (M+) adds the current number to the memory.

func (*Engine) MemoryClear

func (e *Engine) MemoryClear()

MemoryClear (MC) empties the memory.

func (*Engine) MemoryRecall

func (e *Engine) MemoryRecall()

MemoryRecall (MR) enters the number in the memory.

func (*Engine) MemorySubtract

func (e *Engine) MemorySubtract()

MemorySubtract (M−) subtracts the current number from the memory.

func (*Engine) Negate

func (e *Engine) Negate()

Negate (±) changes the sign of the current number.

func (*Engine) Operator

func (e *Engine) Operator(op rune)

Operator sets the pending operator, first evaluating the one before it.

func (*Engine) Percent

func (e *Engine) Percent()

Percent: in "a + b %" and "a − b %" b becomes b percent of a, otherwise b / 100.

func (*Engine) Point

func (e *Engine) Point()

Point types the decimal point.

func (*Engine) Reciprocal

func (e *Engine) Reciprocal()

Reciprocal replaces the current number with 1/x.

func (*Engine) SetText

func (e *Engine) SetText(text string) bool

SetText enters a number given as text (pasted, recalled from the history): "1,234.5", "1 234,5" and "-2e3" are accepted. It reports whether the text was a number.

func (*Engine) Sqrt

func (e *Engine) Sqrt()

Sqrt replaces the current number with its square root.

func (*Engine) Square

func (e *Engine) Square()

Square replaces the current number with its square.

func (*Engine) Text

func (e *Engine) Text() string

Text is the current number without separators, for the clipboard.

type HistoryEntry

type HistoryEntry struct {
	Expression string // "12 + 7 ="
	Result     string // "19"
}

HistoryEntry is one finished calculation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL