Author Topic: Formatting Decimal Currency  (Read 3087 times)

0 Members and 1 Guest are viewing this topic.

Offline AtomicSlaughter

  • Newbie
  • Posts: 14
    • View Profile
Formatting Decimal Currency
« on: February 11, 2021, 02:55:27 pm »
So this SUB came about because I'm currently writing an EPOS system, and could not for the life of me get the math to work properly due to floaty math.

The inability for QB64 to track numbers to the decimal place with accuracy and getting sick of having my program write 1.9 to screen as £1.90 i wrote this piece of code to convert the integers into a string to print to screen.

it is a bit lacking as I first have to convert the integer to string, but this simplifies the process some what.

It is very useful to me, hopefully someone else will find it useful in their program.

Code: QB64: [Select]
  1. SUB Currency (a$)
  2.     l = LEN(a$)
  3.     penny$ = RIGHT$(a$, 2)
  4.     IF LEN(penny$) = 1 THEN
  5.         pound$ = "0"
  6.         penny$ = "0" + penny$:
  7.         conv$ = pound$ + "." + penny$
  8.         a$ = conv$
  9.     ELSE
  10.         pound$ = LEFT$(a$, (l - 2))
  11.         conv$ = pound$ + "." + penny$
  12.         a$ = conv$
  13.     END IF
  14.     IF LEN(conv$) = 3 THEN a$ = "0" + conv$

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Formatting Decimal Currency
« Reply #1 on: February 11, 2021, 03:12:45 pm »
I have WinAPI code for this as well. When I get back to a PC I will give it to you.
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Formatting Decimal Currency
« Reply #2 on: February 11, 2021, 03:33:21 pm »
Quote
and could not for the life of me get the math to work properly due to floaty math.

When I was testing Steve's N2S$ (Number to String converter), I was pretty happy. I think it might be used in IDE? because his Math Evaluator is and this is where that came from, if I recall correctly.
Code: QB64: [Select]
  1. _Title "N2S$ testing for a scientific notation remover" 'b+ 2020-09-04
  2. ' source of bug tracked down 2020-09-08
  3. ' looks pretty good except for one little bug fixed now
  4.  
  5. 'It was because i was not dim'd as an integer!!!
  6. Dim i As Integer '<<<<<<<<<<<<<<<<<<<< Oh man!!!!  this was the problem!!! Discovered 2020-09-08
  7. Dim n##
  8. If 0 Then ' < 0 to skip section, 1 to run section
  9.     Dim n As Integer '<<<<<<<<<<< comment out to test single
  10.     n = 1 ' has an odd bug jump -7 to -8 moves right 1 extra 0   fixed!!
  11.     For i = 0 To 20
  12.         Print i,
  13.         Locate CsrLin, 5: Print N2S$(Str$(n * 10 ^ -i));
  14.         Locate CsrLin, 54: Print N2S$(Str$(n * 10 ^ i))
  15.     Next
  16.     Beep
  17.     n## = 1 ' but this OK too
  18.     'n## = 27.0123456789 'OK
  19.     'n## = (1 / 3) 'OK
  20.     For i = 0 To 20
  21.         Print i;
  22.         Locate CsrLin, 5: Print N2S$(Str$(n## * 10 ^ -i));
  23.         Locate CsrLin, 54: Print N2S$(Str$(n## * 10 ^ i))
  24.     Next
  25.     Beep
  26.  
  27. Function N2S$ (EXP$) 'remove scientific Notation to String (~40 LOC)
  28.     'SMcNeill Jan 7, 2020 ref: https://www.qb64.org/forum/index.php?topic=1555.msg112989#msg112989
  29.     'Last Function in code marked Best Answer (removed debug comments and blank lines added these 2 lines.)
  30.     ReDim t$, sign$, l$, r$, r&&
  31.     ReDim As Long dp, dm, ep, em, check1, l, i
  32.     t$ = LTrim$(RTrim$(EXP$))
  33.     If Left$(t$, 1) = "-" Or Left$(t$, 1) = "N" Then sign$ = "-": t$ = Mid$(t$, 2)
  34.     dp = InStr(t$, "D+"): dm = InStr(t$, "D-")
  35.     ep = InStr(t$, "E+"): em = InStr(t$, "E-")
  36.     check1 = Sgn(dp) + Sgn(dm) + Sgn(ep) + Sgn(em)
  37.     If check1 < 1 Or check1 > 1 Then N2S = _Trim$(EXP$): Exit Function 'If no scientic notation is found, or if we find more than 1 type, it's not SN!
  38.     Select Case l 'l now tells us where the SN starts at.
  39.         Case Is < dp: l = dp
  40.         Case Is < dm: l = dm
  41.         Case Is < ep: l = ep
  42.         Case Is < em: l = em
  43.     End Select
  44.     l$ = Left$(t$, l - 1) 'The left of the SN
  45.     r$ = Mid$(t$, l + 1): r&& = Val(r$) 'The right of the SN, turned into a workable long
  46.     If InStr(l$, ".") Then 'Location of the decimal, if any
  47.         If r&& > 0 Then
  48.             r&& = r&& - Len(l$) + 2
  49.         Else
  50.             r&& = r&& + 1
  51.         End If
  52.         l$ = Left$(l$, 1) + Mid$(l$, 3)
  53.     End If
  54.     Select Case r&&
  55.         Case 0 'what the heck? We solved it already?
  56.             'l$ = l$
  57.         Case Is < 0
  58.             For i = 1 To -r&&
  59.                 l$ = "0" + l$
  60.             Next
  61.             l$ = "." + l$
  62.         Case Else
  63.             For i = 1 To r&&
  64.                 l$ = l$ + "0"
  65.             Next
  66.             l$ = l$
  67.     End Select
  68.     N2S$ = sign$ + l$
  69.  

After converting, just find decimal place and add (00's) | remove excess digits.
« Last Edit: February 11, 2021, 03:39:08 pm by bplus »