Not the farm problem but mine Solved

Post your Gambas programming questions here.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Not the farm problem but mine Solved

Post by cogier »

I used a test value of 89726.23, and it came back with 22 Cents.

I have added a little code to rectify this at line 6. I also change 0 Cents to 'only'. This may be a cultural thing, but that's how we do it this side of the pond, not that I have written a cheque in years. It's all done by BACS now!
Public Sub GetText(iValue As Float) As String

  Dim sNumbers As String[][] = [["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"], ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eightteen", "nineteen"], ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]]

  Dim sValue As String = Str(Fix(iValue))
  Dim icents As Integer = Int((Frac(iValue) * 100) + 0.01) ''Changed to stop rounding down
  Dim iLen As Integer = Len(sValue)
  Dim sResult, sCents As String ''Added sCents

  Dim $ones As Integer = 0
  Dim $teens As Integer = 1
  Dim $over19 As Integer = 2

  If iLen = 5 Then
    If Val(Mid(sValue, 1, 2)) < 20 Then sResult = sNumbers[$teens][Val(Mid(sValue, 2, 1))]
    If Val(Mid(sValue, 1, 2)) > 19 Then sResult = sNumbers[$over19][Val(Mid(sValue, 1, 1))] & " " & sNumbers[$ones][Val(Mid(sValue, 2, 1))]
    sResult &= " thousand "
    svalue = Right(svalue, 3)
    iLen = 3
  Endif

  If iLen = 4 Then
    sResult &= sNumbers[$ones][Val(Left(sValue, 1))] & " thousand "
    svalue = Right(svalue, 3)
    iLen = 3
  Endif

  If iLen = 3 And Val(Left(sValue, 1)) Then
    sResult &= sNumbers[$ones][Val(Left(sValue, 1))] & " hundred "
    svalue = Right(svalue, 2)
    iLen = 2
  Endif

  If iLen = 2 Then
    If Val(Right(sValue, 2)) < 20 Then sResult &= sNumbers[$teens][Val(Mid(sValue, 2, 1))]
    If Val(Right(svalue, 2)) > 19 Then sResult &= sNumbers[$over19][Val(Mid(sValue, 1, 1))] & " " & sNumbers[$ones][Val(Mid(sValue, 2, 1))]
  Endif
  If iLen = 1 Then sResult &= sNumbers[$ones][Val(sValue)]

  If iCents = 0 Then sCents = "only" Else sCents = "and " & Str(iCents) & " cents" ''Added

  Return "* * * " & UCase(sResult[0]) & Mid(sResult, 2) & " dollars " & sCents & " * * *" ''Changed

End
User avatar
grayghost4
Posts: 174
Joined: Wednesday 5th December 2018 5:00am
Location: Marengo, Illinois usa

Re: Not the farm problem but mine Solved

Post by grayghost4 »

I used a test value of 89726.23, and it came back with 22 Cents.
I had that happen one time in my testing also and could not get to happen again ... I also added the + .01 for a while then removed it, thinking it was a Quirk ;)

But I did it inside the brackets
 Dim icent As Integer = Int(Frac(iValue) * 100.01)
Not being a math expert ...I don't know which way would be best ... ( that is why the books say DO NOT USE FLOAT FOR FINANCE )
I think I will change the input to use a string to prevent that. :D

I also use electronic payment system for most things .... this software is more of a learning experience for me to learn Gambas

Thank You for the feedback
Marv
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Not the farm problem but mine Solved

Post by cogier »

Not being a math expert ...I don't know which way would be best ... ( that is why the books say DO NOT USE FLOAT FOR FINANCE )
I think I will change the input to use a string to prevent that. :D
I think you are correct. Maybe you should take your iValue and multiply it by 100 before you start so that everything is an integer?
User avatar
grayghost4
Posts: 174
Joined: Wednesday 5th December 2018 5:00am
Location: Marengo, Illinois usa

Re: Not the farm problem but mine Solved

Post by grayghost4 »

In my program above I used a DIM statement :
  Dim $ones As Integer = 0
  Dim $teens As Integer = 1
  Dim $over19 As Integer = 2
 
Later I found i could do the same thing with :
Dim $ones As Integer = 0, $teens As Integer = 1, $over19 As Integer = 2 
Now I came across the command ENUM:
Enum $ones, $teens, $over19
Which does the same thing if I use it in the Declarations in the beginning of the program, But will not work if I try to put it in the sub, I get a Syntax error ...
Any help from anyone ?

https://gambas-buch.de/dwen/doku.php?id=k7:k7.1:start
User avatar
PJBlack
Posts: 184
Joined: Tuesday 9th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: Not the farm problem but mine Solved

Post by PJBlack »

Enumeration declaration

{ PUBLIC | PRIVATE } ENUM Identifier [ = Value ] [ , Identifier [ = Value ] ... ]

This keyword declares an enumeration, i.e. a list of integer constants.
If the Value of a constant is not specified, then it will be the value of the previous constant plus one, or zero for the first constant.
All constants are accessible everywhere in the class they are declared.
If the PUBLIC keyword is specified, they are also accessible to the other classes having a reference to an object of this class.
[Gambas-Wiki]
Post Reply