How to resolve the algorithm Formatted numeric output step by step in the Amazing Hopper programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Formatted numeric output step by step in the Amazing Hopper programming language

Table of Contents

Problem Statement

Express a number in decimal as a fixed-length string with leading zeros.

For example, the number   7.125   could be expressed as   00007.125.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Formatted numeric output step by step in the Amazing Hopper programming language

Source code in the amazing programming language

#include 

#proto padzeros(_X_,_S_,_D_)
#proto padmoney(_N_,_D_,_F_,_S_,_P_)

algoritmo

     n={}, '125, 12.39802, 0.0003221, -0.0045, 457897.789' enlistar en 'n'

     fijar separador 'NL'
     imprimir( "Express a number in decimal as a fixed-length string with leading zeros:\n\n",\
               #(pad zeros(n,19,5)), "\n\n",\
               #(pad zeros(n,19,0)), "\n\n",\
               #(pad zeros( (-9873000155.584),19,1)), "\n\n",\
               "Bonus track:\n\n",\
               #(lpad( " ",20,string(n))), "\n\n",\
               #(lpad( " ",20,notation(n))),"\n\n",\
               #(pad money(n,1,".",19,"$")),"\n\n",\
               #(pad money(1980.67,1,"_",16,"USD$"),NL))

terminar

subrutinas

pad zeros(n, l, d)
     decimales 'd'
     s=0, sgn=0
     #( s = string( n * sgn:=(sign(n)) ) )
     #( sgn = string(sgn) )
     si ( s, es matriz? )
         mapear( #(sgn=="-1"), "-", sgn )
         mapear( #(sgn=="1"), " ", sgn )
     sino
         #(sgn=="-1"), entonces{ "-",mover a 'sgn' }
         #(sgn=="1"), entonces{ " ",mover a 'sgn' }
     fin si
     #(cat( sgn, lpad( "0",l,s)))
     decimales normales
retornar

pad money(num, dec, fill, size, prefix)
     #(cat(prefix,lpad(fill,size,money(dec,num))))
retornar


  

You may also check:How to resolve the algorithm Count occurrences of a substring step by step in the Wortel programming language
You may also check:How to resolve the algorithm Integer sequence step by step in the Component Pascal programming language
You may also check:How to resolve the algorithm SHA-256 step by step in the Crystal programming language
You may also check:How to resolve the algorithm Order two numerical lists step by step in the Yabasic programming language
You may also check:How to resolve the algorithm Levenshtein distance/Alignment step by step in the Kotlin programming language