~/bend-docscommunity

src/fixed.bend checks

raw source on the hub · import emerging-ezjson@1.3.0.0/src/fixed.bend as Fixed

src/fixed: a JSON number's text written with a fixed count of decimals. The digits are rounded half to even as decimal digits, never through a binary float. The text is split at its e (or E) and its .; the exponent moves the point; the digits past the last kept place decide whether the kept ones go up by one; the integer part loses its leading zeros. A - is kept even when the rounded value is zero, as C's printf and Python's '%.Nf' keep it.

2 imports
import Base
import ./value.bend as V

Types

type Dec source · line 13 · raw

Data

where a number's point is: the digits before it, last digit first, and the digits after it

Definitions

def mark.e source · line 19 · raw

@+ch:Char -> Bool

an exponent mark, e or E

def mark.dot source · line 23 · raw

@+ch:Char -> Bool

a decimal point

def cut.put source · line 27 · raw

@ch:Char -> @pair:Pair(String, String) -> Pair(String, String)

put a character before the first of a pair

def cut.at source · line 32 · raw

@hit:Bool -> @ch:Char -> @after:String -> @more:(@_:Unit -> Pair(String, String)) -> Pair(String, String)

at a mark the cut is made; any other character is kept before it

def cut.e source · line 45 · raw

@txt:String -> Pair(String, String)

a text cut at its first exponent mark: what is before it, and after it

def cut.dot source · line 53 · raw

@txt:String -> Pair(String, String)

a text cut at its first decimal point: what is before it, and after it

def esign.at source · line 61 · raw

@minus:Bool -> @plus:Bool -> @ch:Char -> @tl:String -> Pair(Bool, String)

an exponent's sign and digits: - makes it negative, + is dropped

def esign source · line 71 · raw

@txt:String -> Pair(Bool, String)

an exponent's sign and digits

def parts.of source · line 79 · raw

@cut:Pair(String, String) -> Pair(Pair(String, String), Pair(Bool, String))

the parts of a text cut at its exponent mark

def parts source · line 85 · raw

@txt:String -> Pair(Pair(String, String), Pair(Bool, String))

an unsigned number's parts: its integer and fraction digits, and its exponent's sign and digits

def digit.nat source · line 91 · raw

@ch:Char -> Nat

a digit's value

def nat.go source · line 95 · raw

@txt:String -> @acc:Nat -> Nat

decimal digits read onto acc, most significant first

def nat source · line 103 · raw

@txt:String -> Nat

decimal digits' value

def up source · line 108 · raw

@dec:Dec -> Dec

the point one place right: the first fraction digit, or a 0 past the last, moves before the point

def down source · line 119 · raw

@dec:Dec -> Dec

the point one place left: the last integer digit, or a 0 before the first, moves after the point

def ups source · line 129 · raw

@kk:Nat -> @dec:Dec -> Dec

the point kk places right

def downs source · line 137 · raw

@kk:Nat -> @dec:Dec -> Dec

the point kk places left

def move source · line 145 · raw

@neg:Bool -> @kk:Nat -> @dec:Dec -> Dec

the digits with the point moved by the exponent

def place source · line 153 · raw

@pts:Pair(Pair(String, String), Pair(Bool, String)) -> Dec

the parts as digits around the point, the point moved by the exponent

def pad source · line 160 · raw

@nn:Nat -> @right:String -> Pair(String, String)

the first nn fraction digits, 0s past the last, and the digits dropped

def nonzero.at source · line 172 · raw

@zero:Bool -> @more:(@_:Unit -> Bool) -> Bool

a 0 digit leaves the answer to the rest; any other digit is nonzero

def nonzero source · line 180 · raw

@txt:String -> Bool

whether any digit is not 0

def odd source · line 188 · raw

@kept:String -> Bool

whether the last kept digit is odd; no digit is an even 0

def tie source · line 197 · raw

@sticky:Bool -> @kept:String -> Bool

a first dropped digit of 5 with no other nonzero digit dropped is a tie, which goes to the even neighbour

def round.at source · line 205 · raw

@order:Cmp -> @rest:String -> @kept:String -> Bool

the first dropped digit against 5

def round.up source · line 215 · raw

@dropped:String -> @kept:String -> Bool

whether the kept digits (last first) go up by one, from the dropped ones

def inc.at source · line 223 · raw

@nine:Bool -> @ch:Char -> @tl:String -> @more:(@_:Unit -> String) -> String

one more on a digit, carried: a 9 is 0 and carries

def inc source · line 231 · raw

@digits:String -> String

one more on digits written last first

def bump source · line 239 · raw

@go_up:Bool -> @kept:String -> String

the kept digits, one more when they round up

def round.keep source · line 247 · raw

@+left:String -> @pair:Pair(String, String) -> String

the kept digits split at the last place

def round source · line 253 · raw

@+nn:Nat -> @dec:Dec -> String

every digit to the last kept place, last first, rounded half to even

def trim.at source · line 261 · raw

@zero:Bool -> @ch:Char -> @tl:String -> @more:(@_:Unit -> String) -> String

an integer part with its leading 0s dropped; no digit is 0

def trim source · line 269 · raw

@int:String -> String

an integer part with its leading 0s dropped; no digit is 0

def point source · line 277 · raw

@nn:Nat -> @int:String -> @frac:String -> String

the integer part, then a point and the fraction when there is one

def write source · line 285 · raw

@+nn:Nat -> @+digits:String -> String

rounded digits, last first, written with nn decimals

def unsigned source · line 290 · raw

@txt:String -> @+nn:Nat -> String

an unsigned number's text with nn decimals

def text.at source · line 294 · raw

@minus:Bool -> @ch:Char -> @tl:String -> @+nn:Nat -> String

a sign kept in front of the unsigned rest

def text source · line 302 · raw

@txt:String -> @+nn:Nat -> String

a number's text with nn decimals

def fixed.go source · line 310 · raw

@ok:Bool -> @raw:String -> @+nn:Nat -> Maybe<&2, String>

Some of the text with nn decimals when ok, else None

def fixed source · line 318 · raw

@+raw:String -> @+nn:Nat -> Maybe<&2, String>

a JSON number's text with nn decimals, or None when it is not one