How to resolve the algorithm Suffixation of decimal numbers step by step in the Go programming language
How to resolve the algorithm Suffixation of decimal numbers step by step in the Go 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 Go programming language
This Go program takes in a number and displays it with a suffix like "K" for thousand, "M" for million, etc. It also supports binary (base 2) numbers and can specify the number of fractional digits to display.
- The
googol
function calculates a googol, which is10^100
. - The
suffixe
function takes a string representing a number and parses it into abig.Float
value. - It then determines the appropriate suffix to use based on the magnitude of the number and whether it's binary or decimal.
- Finally, it prints the formatted number with the suffix.
In the main
function, the program tests the suffixe
function with various input strings representing numbers.
Here's an example of the output for the input "456,789,100,000,000"
:
input number = 456,789,100,000,000
fraction digs = 0
specified radix = 10
new number = 456.789T
This indicates that the input number is 456.789 trillion.
Source code in the go programming language
package main
import (
"fmt"
"math/big"
"strconv"
"strings"
)
var suffixes = " KMGTPEZYXWVU"
var ggl = googol()
func googol() *big.Float {
g1 := new(big.Float).SetPrec(500)
g1.SetInt64(10000000000)
g := new(big.Float)
g.Set(g1)
for i := 2; i <= 10; i++ {
g.Mul(g, g1)
}
return g
}
func suffize(arg string) {
fields := strings.Fields(arg)
a := fields[0]
if a == "" {
a = "0"
}
var places, base int
var frac, radix string
switch len(fields) {
case 1:
places = -1
base = 10
case 2:
places, _ = strconv.Atoi(fields[1])
base = 10
frac = strconv.Itoa(places)
case 3:
if fields[1] == "," {
places = 0
frac = ","
} else {
places, _ = strconv.Atoi(fields[1])
frac = strconv.Itoa(places)
}
base, _ = strconv.Atoi(fields[2])
if base != 2 && base != 10 {
base = 10
}
radix = strconv.Itoa(base)
}
a = strings.Replace(a, ",", "", -1) // get rid of any commas
sign := ""
if a[0] == '+' || a[0] == '-' {
sign = string(a[0])
a = a[1:] // remove any sign after storing it
}
b := new(big.Float).SetPrec(500)
d := new(big.Float).SetPrec(500)
b.SetString(a)
g := false
if b.Cmp(ggl) >= 0 {
g = true
}
if !g && base == 2 {
d.SetUint64(1024)
} else if !g && base == 10 {
d.SetUint64(1000)
} else {
d.Set(ggl)
}
c := 0
for b.Cmp(d) >= 0 && c < 12 { // allow b >= 1K if c would otherwise exceed 12
b.Quo(b, d)
c++
}
var suffix string
if !g {
suffix = string(suffixes[c])
} else {
suffix = "googol"
}
if base == 2 {
suffix += "i"
}
fmt.Println(" input number =", fields[0])
fmt.Println(" fraction digs =", frac)
fmt.Println("specified radix =", radix)
fmt.Print(" new number = ")
if places >= 0 {
fmt.Printf("%s%.*f%s\n", sign, places, b, suffix)
} else {
fmt.Printf("%s%s%s\n", sign, b.Text('g', 50), suffix)
}
fmt.Println()
}
func main() {
tests := []string{
"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 := range tests {
suffize(test)
}
}
You may also check:How to resolve the algorithm Sum of squares step by step in the REXX programming language
You may also check:How to resolve the algorithm Interactive programming (repl) step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Variable size/Set step by step in the Z80 Assembly programming language
You may also check:How to resolve the algorithm Proper divisors step by step in the Erlang programming language
You may also check:How to resolve the algorithm Proper divisors step by step in the zkl programming language