How to resolve the algorithm Number names step by step in the V (Vlang) programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Number names step by step in the V (Vlang) programming language
Table of Contents
Problem Statement
Show how to spell out a number in English. You can use a preexisting implementation or roll your own, but you should support inputs up to at least one million (or the maximum value of your language's default bounded integer type, if that's less). Support for inputs other than positive integers (like zero, negative integers, and floating-point numbers) is optional.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Number names step by step in the V (Vlang) programming language
Source code in the v programming language
const (
small = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
illions = ["", " thousand", " million", " billion", " trillion", " quadrillion", " quintillion"]
)
fn say(n i64) string {
mut t := ''
mut nn := n
if n < 0 {
t = "negative "
// Note, for math.MinInt64 this leaves n negative.
nn = -n
}
if nn < 20 {
t += small[nn]
}
else if nn < 100 {
t += tens[nn/10]
s := nn % 10
if s > 0 {
t += "-" + small[s]
}
}
else if nn < 1000 {
t += small[nn/100] + " hundred"
s := nn % 100
if s > 0 {
t += " " + say(s)
}
}
else {
// work right-to-left
mut sx := ""
for i := 0; nn > 0; i++ {
p := nn % 1000
nn /= 1000
if p > 0 {
mut ix := say(p) + illions[i]
if sx != "" {
ix += " " + sx
}
sx = ix
}
}
t += sx
}
return t
}
fn main(){
mut nums := []i64{}
nums = [i64(12), i64(1048576), i64(9e18), i64(-2), i64(0)]
for n in nums {
println(say(n))
}
}
You may also check:How to resolve the algorithm Semiprime step by step in the Prolog programming language
You may also check:How to resolve the algorithm Execute a system command step by step in the LFE programming language
You may also check:How to resolve the algorithm Dinesman's multiple-dwelling problem step by step in the Racket programming language
You may also check:How to resolve the algorithm CRC-32 step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Terminal control/Unicode output step by step in the R programming language