How to resolve the algorithm Suffixation of decimal numbers step by step in the Wren programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Suffixation of decimal numbers step by step in the Wren programming language
Table of Contents
Problem Statement
Suffixation: a letter or a group of letters added to the end of a word to change its meaning. Suffixation: the addition of a metric or "binary" metric suffix to a number, with/without rounding.
Write a function(s) to append (if possible) a metric or a "binary" metric suffix to a number (displayed in decimal). The number may be rounded (as per user specification) (via shortening of the number when the number of digits past the decimal point are to be used).
would display:
Use whatever parameterizing your computer language supports, and it's permitted to create as many separate functions as are needed (if needed) if function arguments aren't allowed to be omitted or varied.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Suffixation of decimal numbers step by step in the Wren programming language
Source code in the wren programming language
import "/big" for BigRat
import "/fmt" for Fmt
var suffixes = " KMGTPEZYXWVU"
var googol = BigRat.fromDecimal("1e100")
var suffize = Fn.new { |arg|
var fields = arg.split(" ").where { |s| s != "" }.toList
if (fields.isEmpty) fields.add("0")
var a = fields[0]
var places
var base
var frac = ""
var radix = ""
var fc = fields.count
if (fc == 1) {
places = -1
base = 10
} else if (fc == 2) {
places = Num.fromString(fields[1])
base = 10
frac = places.toString
} else if (fc == 3) {
if (fields[1] == ",") {
places = 0
frac = ","
} else {
places = Num.fromString(fields[1])
frac = places.toString
}
base = Num.fromString(fields[2])
if (base !=2 && base != 10) base = 10
radix = base.toString
}
a = a.replace(",", "") // get rid of any commas
var sign = ""
if (a[0] == "+" || a[0] == "-") {
sign = a[0]
a = a[1..-1] // remove any sign after storing it
}
var b = BigRat.fromDecimal(a)
var g = b >= googol
var d = (!g && base == 2) ? BigRat.new(1024, 1) :
(!g && base == 10) ? BigRat.new(1000, 1) : googol.copy()
var c = 0
while (b >= d && c < 12) { // allow b >= 1K if c would otherwise exceed 12
b = b / d
c = c + 1
}
var suffix = !g ? suffixes[c] : "googol"
if (base == 2) suffix = suffix + "i"
System.print(" input number = %(fields[0])")
System.print(" fraction digs = %(frac)")
System.print("specified radix = %(radix)")
System.write(" new number = ")
BigRat.showAsInt = true
if (places >= 0) {
Fmt.print("$0s$s$s", sign, b.toDecimal(places), suffix)
} else {
Fmt.print("$0s$s$s", sign, b.toDecimal, suffix)
}
System.print()
}
var tests = [
"87,654,321",
"-998,877,665,544,332,211,000 3",
"+112,233 0",
"16,777,216 1",
"456,789,100,000,000",
"456,789,100,000,000 2 10",
"456,789,100,000,000 5 2",
"456,789,100,000.000e+00 0 10",
"+16777216 , 2",
"1.2e101",
"446,835,273,728 1",
"1e36",
"1e39", // there isn't a big enough suffix for this one but it's less than googol
]
for (test in tests) suffize.call(test)
You may also check:How to resolve the algorithm Find the intersection of two lines step by step in the zkl programming language
You may also check:How to resolve the algorithm MD5/Implementation step by step in the Perl programming language
You may also check:How to resolve the algorithm Perfect numbers step by step in the J programming language
You may also check:How to resolve the algorithm Stack step by step in the Python programming language
You may also check:How to resolve the algorithm Range expansion step by step in the Action! programming language