How to resolve the algorithm Number names step by step in the HicEst programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Number names step by step in the HicEst 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 HicEst programming language

Source code in the hicest programming language

SUBROUTINE NumberToWords(number)
 CHARACTER outP*255, small*130, tens*80, big*80
 REAL ::   decimal_places = 7
 INIT( APPENDIX("#literals"), small, tens, big)

 num = ABS( INT(number) )
 order = 0
 outP = ' '
 DO i = 1, num + 1
   tmp = MOD(num, 100)
   IF(tmp > 19) THEN
       EDIT(Text=tens, ITeM=INT(MOD(tmp/10, 10)), Parse=medium)
       IF( MOD(tmp, 10) ) THEN
           EDIT(Text=small, ITeM=MOD(tmp,10)+1, Parse=mini)
           outP = medium // '-' // mini // ' ' // outP
       ELSE
           outP = medium // ' ' // outP
       ENDIF
   ELSEIF(tmp > 0) THEN
       EDIT(Text=small, ITeM=tmp+1, Parse=mini)
       outP = mini // ' '// outP
   ELSEIF(number == 0) THEN
       outP = 'zero'
   ENDIF

   tmp = INT(MOD(num, 1000) / 100)
   IF(tmp) THEN
       EDIT(Text=small, ITeM=tmp+1, Parse=oneto19)
       outP = oneto19 // ' hundred ' // outP
   ENDIF

   num = INT(num /1000)
   IF( num == 0) THEN
       IF(number < 0) outP = 'minus ' // outP
       fraction = ABS( MOD(number, 1) )
       IF(fraction) WRITE(Text=outP, APPend) ' point'
       DO j = 1, decimal_places
         IF( fraction >= 10^(-decimal_places) ) THEN
             num = INT( 10.01 * fraction )
             EDIT(Text=small, ITeM=num+1, Parse=digit)
             WRITE(Text=outP, APPend) ' ', digit
             fraction = 10*fraction - num
         ENDIF
       ENDDO
       OPEN(FIle="temp.txt", APPend)
       WRITE(FIle="temp.txt", Format='F10, " = ", A', CLoSe=1) number, outP
       RETURN
   ENDIF

   order = order + 1
   EDIT(Text=big, ITeM=order, Parse=kilo)
   IF( MOD(num, 1000) ) outP = kilo // ' and '// outP
 ENDDO
END

CALL NumberToWords( 0 )
CALL NumberToWords( 1234 )
CALL NumberToWords( 1234/100 )
CALL NumberToWords( 10000000 + 1.2 )
CALL NumberToWords( 2^15 )
CALL NumberToWords( 0.001 )
CALL NumberToWords( -EXP(1) )

#literals
 SMALL= zero one two three four five six seven eight nine ten &
 eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen

 TENS=ten twenty thirty forty fifty sixty seventy eighty ninety

 BIG=thousand million billion trillion quadrillion

0           = zero
1234        = one thousand and two hundred thirty-four
12.34       = twelve point three four
10000001.2  = ten million and one point two
32768       = thirty-two thousand and seven hundred sixty-eight
1E-3        =  point zero zero one
-2.7182818  = minus two point seven one eight two eight one eight

  

You may also check:How to resolve the algorithm Read a file line by line step by step in the Ring programming language
You may also check:How to resolve the algorithm Tau number step by step in the R programming language
You may also check:How to resolve the algorithm Truncate a file step by step in the PowerBASIC programming language
You may also check:How to resolve the algorithm Flipping bits game step by step in the Red programming language
You may also check:How to resolve the algorithm Quaternion type step by step in the FreeBASIC programming language