Author Topic: Need help with decimal point.  (Read 4557 times)

0 Members and 1 Guest are viewing this topic.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Need help with decimal point.
« on: July 27, 2020, 05:28:34 pm »
I am very close to finishing my first simple calculator, which uses the mouse. But I can't seem to figure out how to add JUST a decimal point to a variable. I know QB64 rounds  .0 to 0 with VAL which is my problem. Here is the decimal point code I have so far. I just need a way to click the decimal point button on the calculator to be able to add it. I found some more advanced calculators on the forum but I still can't figure it out. When I'm done with the calculator I'll make a new topic for it. I have been working on the calculator for a couple days.

Code: QB64: [Select]
  1.         IF mouseX > 126 AND mouseX < 200 AND mouseY > 295 AND mouseY < 350 THEN
  2.             num$ = STR$(num) + "."
  3.             num = VAL(num$)
  4.             buttonx = 126: buttony = 295
  5.             GOSUB press:
  6.             GOSUB number:
  7.         END IF
  8.  
« Last Edit: July 27, 2020, 05:29:36 pm by SierraKen »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Need help with decimal point.
« Reply #1 on: July 27, 2020, 06:19:34 pm »
Try keeping all your numbers in string form until you need to do the math then VAL(the number).

Then for displaying the number use a function that sets the number of decimal places to display:

Code: QB64: [Select]
  1. PRINT xDP$(11, 5)
  2. PRINT xDP$(0, 2)
  3. PRINT xDP$(1, 0)
  4. PRINT xDP$(0, 0)
  5. PRINT xDP$(RND * 100, 2)
  6.  
  7. FUNCTION xDP$ (x, DP)  ' x is the number to display assuming default Single, DP = decimal places
  8.     DIM test$, p AS INTEGER
  9.     test$ = _TRIM$(STR$(INT(x * 10 ^ DP) / 10 ^ DP))
  10.     p = INSTR(test$, ".")
  11.     IF p = 0 AND DP <> 0 THEN test$ = test$ + "." + STRING$(DP, "0")
  12.     IF p AND DP = 0 THEN test$ = LEFT$(test$, LEN(test$) - 1)
  13.     xDP$ = test$

EDIT: updated function with more tests
« Last Edit: July 27, 2020, 07:21:36 pm by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Need help with decimal point.
« Reply #2 on: July 27, 2020, 07:52:41 pm »
That gives me a decimal point and places with zero, which is nice. But I'm lost on how to physically add the decimal point to an existing variable in the location of the program that it needs to be in. I am probably missing something really simple. Math with a decimal is fine, but pressing the decimal button to add it to the beginning number is what I'm lost on. If you want to take a look at it, it's here. If I can get this fixed, the whole calculator will be done. Thanks for the help.

Code: QB64: [Select]
  1. _TITLE "Calculator"
  2. SCREEN _NEWIMAGE(400, 400, 32)
  3. 'Setup calculator
  4. LINE (50, 25)-(265, 50), _RGB32(255, 255, 255), B
  5. LINE (50, 75)-(350, 350), _RGB32(255, 255, 255), B
  6. FOR buttony = 75 TO 295 STEP 55
  7.     FOR buttonx = 50 TO 275 STEP 75
  8.         FOR bb = 0 TO 10
  9.             c = c + 10
  10.             LINE (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(100 + c, 100 + c, 100 + c), B
  11.         NEXT bb
  12.         PAINT (buttonx + 12, buttony + 12), _RGB32(100 + c, 100 + c, 100 + c)
  13.         c = 0
  14.     NEXT buttonx
  15. NEXT buttony
  16. buttonx = 275: buttony = 20
  17. FOR bb = 0 TO 10
  18.     c = c + 10
  19.     LINE (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(50, 100 + c, 50), B
  20. NEXT bb
  21. PAINT (buttonx + 12, buttony + 12), _RGB32(50, 100 + c, 50)
  22. COLOR _RGB32(0, 0, 0), _RGB32(50, 100 + c, 50)
  23. c = 0
  24. _PRINTSTRING (312, 47), "C"
  25. COLOR _RGB32(0, 0, 0), _RGB32(210, 210, 210)
  26. _PRINTSTRING (87, 102), CHR$(251) 'square root
  27. _PRINTSTRING (152, 102), "sin"
  28. _PRINTSTRING (227, 102), "cos"
  29. _PRINTSTRING (302, 102), "tan"
  30. _PRINTSTRING (87, 157), "7"
  31. _PRINTSTRING (162, 157), "8"
  32. _PRINTSTRING (237, 157), "9"
  33. _PRINTSTRING (312, 157), "/"
  34. _PRINTSTRING (87, 212), "4"
  35. _PRINTSTRING (162, 212), "5"
  36. _PRINTSTRING (237, 212), "6"
  37. _PRINTSTRING (312, 212), "x"
  38. _PRINTSTRING (87, 267), "1"
  39. _PRINTSTRING (162, 267), "2"
  40. _PRINTSTRING (237, 267), "3"
  41. _PRINTSTRING (312, 267), "-"
  42. _PRINTSTRING (87, 322), "0"
  43. _PRINTSTRING (162, 322), "."
  44. _PRINTSTRING (237, 322), "="
  45. _PRINTSTRING (312, 322), "+"
  46. num = 0
  47. GOSUB number:
  48.     _LIMIT 20
  49.         mouseX = _MOUSEX
  50.         mouseY = _MOUSEY
  51.         mouseLeftButton = _MOUSEBUTTON(1)
  52.     LOOP
  53.     IF mouseLeftButton THEN
  54.         mouseLeftButton = 0
  55.         IF mouseX > 275 AND mouseX < 350 AND mouseY > 20 AND mouseY < 75 THEN
  56.             num = 0
  57.             a = 0: s = 0: t = 0: d = 0
  58.             buttonx = 275: buttony = 20
  59.             GOSUB zero:
  60.             GOSUB number:
  61.         END IF
  62.         IF mouseX > 50 AND mouseX < 125 AND mouseY > 75 AND mouseY < 130 THEN
  63.             IF num < 0 THEN GOTO skip1:
  64.             num = SQR(num)
  65.             skip1:
  66.             buttonx = 50: buttony = 75
  67.             GOSUB press:
  68.             GOSUB number:
  69.         END IF
  70.         IF mouseX > 126 AND mouseX < 200 AND mouseY > 75 AND mouseY < 130 THEN
  71.             num = SIN(num)
  72.             buttonx = 126: buttony = 75
  73.             GOSUB press:
  74.             GOSUB number:
  75.         END IF
  76.         IF mouseX > 200 AND mouseX < 275 AND mouseY > 75 AND mouseY < 130 THEN
  77.             num = COS(num)
  78.             buttonx = 200: buttony = 75
  79.             GOSUB press:
  80.             GOSUB number:
  81.         END IF
  82.         IF mouseX > 275 AND mouseX < 350 AND mouseY > 75 AND mouseY < 130 THEN
  83.             num = TAN(num)
  84.             buttonx = 275: buttony = 75
  85.             GOSUB press:
  86.             GOSUB number:
  87.         END IF
  88.         IF mouseX > 50 AND mouseX < 125 AND mouseY > 130 AND mouseY < 185 THEN
  89.             num$ = STR$(num) + "7"
  90.             num = VAL(num$)
  91.             IF neg = 1 THEN num = -num: neg = 0
  92.             buttonx = 50: buttony = 130
  93.             GOSUB press:
  94.             GOSUB number:
  95.         END IF
  96.         IF mouseX > 126 AND mouseX < 200 AND mouseY > 130 AND mouseY < 185 THEN
  97.             num$ = STR$(num) + "8"
  98.             num = VAL(num$)
  99.             IF neg = 1 THEN num = -num: neg = 0
  100.             buttonx = 126: buttony = 130
  101.             GOSUB press:
  102.             GOSUB number:
  103.         END IF
  104.         IF mouseX > 200 AND mouseX < 275 AND mouseY > 130 AND mouseY < 185 THEN
  105.             num$ = STR$(num) + "9"
  106.             num = VAL(num$)
  107.             IF neg = 1 THEN num = -num: neg = 0
  108.             buttonx = 200: buttony = 130
  109.             GOSUB press:
  110.             GOSUB number:
  111.         END IF
  112.         IF mouseX > 275 AND mouseX < 350 AND mouseY > 130 AND mouseY < 185 THEN
  113.             IF d = 0 THEN
  114.                 d = 1: a = 0: s = 0: t = 0
  115.                 oldnum = num
  116.                 num = 0
  117.                 GOTO nex1:
  118.             END IF
  119.             IF d = 1 AND num <> 0 THEN
  120.                 num = oldnum / num
  121.                 d = 0
  122.             END IF
  123.             nex1:
  124.             buttonx = 275: buttony = 130
  125.             GOSUB press:
  126.             GOSUB number:
  127.         END IF
  128.         IF mouseX > 50 AND mouseX < 125 AND mouseY > 185 AND mouseY < 240 THEN
  129.             num$ = STR$(num) + "4"
  130.             num = VAL(num$)
  131.             IF neg = 1 THEN num = -num: neg = 0
  132.             buttonx = 50: buttony = 185
  133.             GOSUB press:
  134.             GOSUB number:
  135.         END IF
  136.         IF mouseX > 126 AND mouseX < 200 AND mouseY > 185 AND mouseY < 240 THEN
  137.             num$ = STR$(num) + "5"
  138.             num = VAL(num$)
  139.             IF neg = 1 THEN num = -num: neg = 0
  140.             buttonx = 126: buttony = 185
  141.             GOSUB press:
  142.             GOSUB number:
  143.         END IF
  144.         IF mouseX > 200 AND mouseX < 275 AND mouseY > 185 AND mouseY < 240 THEN
  145.             num$ = STR$(num) + "6"
  146.             num = VAL(num$)
  147.             IF neg = 1 THEN num = -num: neg = 0
  148.             buttonx = 200: buttony = 185
  149.             GOSUB press:
  150.             GOSUB number:
  151.         END IF
  152.         IF mouseX > 275 AND mouseX < 350 AND mouseY > 185 AND mouseY < 240 THEN
  153.             IF t = 0 THEN
  154.                 d = 0: a = 0: s = 0: t = 1
  155.                 oldnum = num
  156.                 num = 0
  157.                 GOTO nex2:
  158.             END IF
  159.             IF t = 1 THEN
  160.                 num = oldnum * num
  161.                 t = 0
  162.             END IF
  163.             nex2:
  164.             buttonx = 275: buttony = 185
  165.             GOSUB press:
  166.             GOSUB number:
  167.         END IF
  168.         IF mouseX > 50 AND mouseX < 125 AND mouseY > 240 AND mouseY < 295 THEN
  169.             num$ = STR$(num) + "1"
  170.             num = VAL(num$)
  171.             IF neg = 1 THEN num = -num: neg = 0
  172.             buttonx = 50: buttony = 240
  173.             GOSUB press:
  174.             GOSUB number:
  175.         END IF
  176.         IF mouseX > 126 AND mouseX < 200 AND mouseY > 240 AND mouseY < 295 THEN
  177.             num$ = STR$(num) + "2"
  178.             num = VAL(num$)
  179.             IF neg = 1 THEN num = -num: neg = 0
  180.             buttonx = 126: buttony = 240
  181.             GOSUB press:
  182.             GOSUB number:
  183.         END IF
  184.         IF mouseX > 200 AND mouseX < 275 AND mouseY > 240 AND mouseY < 295 THEN
  185.             num$ = STR$(num) + "3"
  186.             num = VAL(num$)
  187.             IF neg = 1 THEN num = -num: neg = 0
  188.             buttonx = 200: buttony = 240
  189.             GOSUB press:
  190.             GOSUB number:
  191.         END IF
  192.         IF mouseX > 275 AND mouseX < 350 AND mouseY > 240 AND mouseY < 295 THEN
  193.             IF s = 0 THEN
  194.                 IF num = 0 THEN neg = 1: GOTO nex3:
  195.                 d = 0: a = 0: s = 1: t = 0
  196.                 oldnum = num
  197.                 num = 0
  198.                 GOTO nex3:
  199.             END IF
  200.             IF s = 1 THEN
  201.                 num = oldnum - num
  202.                 s = 0
  203.             END IF
  204.             nex3:
  205.             buttonx = 275: buttony = 240
  206.             GOSUB press:
  207.             GOSUB number:
  208.         END IF
  209.         IF mouseX > 50 AND mouseX < 125 AND mouseY > 295 AND mouseY < 350 THEN
  210.             num$ = STR$(num) + "0"
  211.             num = VAL(num$)
  212.             IF neg = 1 THEN num = -num: neg = 0
  213.             buttonx = 50: buttony = 295
  214.             GOSUB press:
  215.             GOSUB number:
  216.         END IF
  217.  
  218.         '-------------------------------------------------------------------------
  219.         'This is the decimal area I am having problems with.
  220.         '-------------------------------------------------------------------------
  221.         IF mouseX > 126 AND mouseX < 200 AND mouseY > 295 AND mouseY < 350 THEN
  222.             buttonx = 126: buttony = 295
  223.             GOSUB press:
  224.             GOSUB number:
  225.         END IF
  226.         '--------------------------------------------------------------------------
  227.  
  228.         IF mouseX > 200 AND mouseX < 275 AND mouseY > 295 AND mouseY < 350 THEN
  229.             IF a = 1 THEN num = oldnum + num: a = 0
  230.             IF s = 1 THEN num = oldnum - num: s = 0
  231.             IF t = 1 THEN num = oldnum * num: t = 0
  232.             IF d = 1 THEN num = oldnum / num: d = 0
  233.             buttonx = 200: buttony = 295
  234.             GOSUB press:
  235.             GOSUB number:
  236.         END IF
  237.         IF mouseX > 275 AND mouseX < 350 AND mouseY > 295 AND mouseY < 350 THEN
  238.             IF a = 0 THEN
  239.                 d = 0: a = 1: s = 0: t = 0
  240.                 oldnum = num
  241.                 num = 0
  242.                 GOTO nex4:
  243.             END IF
  244.             IF a = 1 THEN
  245.                 num = oldnum + num
  246.                 a = 0
  247.             END IF
  248.             nex4:
  249.             buttonx = 275: buttony = 295
  250.             GOSUB press:
  251.             GOSUB number:
  252.         END IF
  253.     END IF
  254. number:
  255. COLOR _RGB32(255, 255, 255), _RGB32(0, 0, 0)
  256. _PRINTSTRING (55, 30), "                          "
  257. 'num$ = STR$(num)
  258. x = num
  259. _PRINTSTRING (55, 30), xDP$(num, 4)
  260. press:
  261. c = 110
  262. FOR bb = 0 TO 10
  263.     c = c - 10
  264.     LINE (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(100 + c, 100 + c, 100 + c), B
  265. NEXT bb
  266. _DELAY .25
  267. FOR bb = 0 TO 10
  268.     c = c + 10
  269.     LINE (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(100 + c, 100 + c, 100 + c), B
  270. NEXT bb
  271. zero:
  272. c = 110
  273. FOR bb = 0 TO 10
  274.     c = c - 10
  275.     LINE (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(50, 100 + c, 50), B
  276. NEXT bb
  277. _DELAY .25
  278. FOR bb = 0 TO 10
  279.     c = c + 10
  280.     LINE (buttonx + bb, buttony + bb)-(buttonx + 75 - bb, buttony + 55 - bb), _RGB32(50, 100 + c, 50), B
  281. NEXT bb
  282.  
  283. FUNCTION xDP$ (x, DP)
  284.     DIM test$, p AS INTEGER
  285.     test$ = _TRIM$(STR$(INT(x * 10 ^ DP) / 10 ^ DP))
  286.     p = INSTR(test$, ".")
  287.     IF p = 0 AND DP <> 0 THEN test$ = test$ + "." + STRING$(DP, "0")
  288.     IF p AND DP = 0 THEN test$ = LEFT$(test$, LEN(test$) - 1)
  289.     xDP$ = test$
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  





Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Need help with decimal point.
« Reply #3 on: July 27, 2020, 08:45:50 pm »
Again like I said, don't convert the number string to a number you are typing in until you are ready to do the math.

Say the number is "12", you click decimal, then number$ = number$ + "." , you click the 9 then number$ = number$ + "9" keep it as a string!

BTW when you click the decimal you should check  if the number has a decimal in it already and not add it into the number$ string if it does have one.

Then when you do the math say add, then num = val(number$) + val(lastNumber$)

Then when you display that number (the sum) use _printstring(x, y), xDP$(num, howManyPlaces)

You can also keep everything right justified using a set width in number display panel. Say 10 places maximum display width use:
_printstring (x, y), right$(space$(10) + number$, 10)  ' <<< for displaying a number build
or _printstring(x, y), right$(space$(10) + xDP$(x, decimals), 10) ' <<< for displaying result of math operation


Remember use these buttons only to build the number string 1,2,3,4,5,6,7,8,9,0, . and neg button to maybe toggle a - sign in front of the number string.
« Last Edit: July 27, 2020, 09:18:20 pm by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Need help with decimal point.
« Reply #4 on: July 27, 2020, 09:14:57 pm »
Hi SierraKen

I can see that you have a revival about spaghetti code!
That's ok, you're trying to use a too versatle tool (GOTO/GOSUB) with no headache!
But please, let me know why do you make no comment to your code?

Well however I must ask to you:
why do you play with num$ and num every time?
why do you display 4 decimal cifers after decimal point from starting time?
-----
feedbacks:
1-  after the calculation the user  cannot modify num by button's input
2- in windows calculation thenumber is on the right side and not at left
Good Coding
PS:
 IMHO you can solve the issue  using a string variable for num, but you must adapt the Bplus function ! Or make another one.
Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Need help with decimal point.
« Reply #5 on: July 27, 2020, 09:16:52 pm »
yep! Bplus has already written the solution and the control issue on decimal point!
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Need help with decimal point.
« Reply #6 on: July 28, 2020, 12:12:44 pm »
How is this coming Ken?

Have you figured the way to simplify your code?

BTW those buttons look great!

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Need help with decimal point.
« Reply #7 on: July 28, 2020, 12:18:20 pm »
Thanks B+ :). I started changing it yesterday but got tired so I'm back at it today. :) I can't promise anything but I'll try it for awhile. Thank you for the help! Your way does logically sound the best.

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Need help with decimal point.
« Reply #8 on: July 28, 2020, 12:57:12 pm »
Indeed, those are nice looking buttons. I need to study those. Great work so far Ken.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Need help with decimal point.
« Reply #9 on: July 28, 2020, 01:11:35 pm »
Thanks OldMoses. I'm getting closer! Man what a puzzle. lol

B+, I had to remove your INT in your Function because every time I did something like 2.5 your code turned it into 2.4999  or something like that. Which is strange, but removing your INT fixed it. I hope to be done with this today.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Need help with decimal point.
« Reply #10 on: July 28, 2020, 02:11:06 pm »
Thanks OldMoses. I'm getting closer! Man what a puzzle. lol

B+, I had to remove your INT in your Function because every time I did something like 2.5 your code turned it into 2.4999  or something like that. Which is strange, but removing your INT fixed it. I hope to be done with this today.

Well INT was absolutely essential to this function as it controls the number of decimals:
Code: QB64: [Select]
  1. FUNCTION xDP$ (x, DP)
  2.     DIM test$, p AS INTEGER
  3.     test$ = _TRIM$(STR$(INT(x * 10 ^ DP) / 10 ^ DP))
  4.     p = INSTR(test$, ".")
  5.     IF p = 0 AND DP <> 0 THEN test$ = test$ + "." + STRING$(DP, "0")
  6.     IF p AND DP = 0 THEN test$ = LEFT$(test$, LEN(test$) - 1)
  7.     xDP$ = test$
  8.  

Maybe you are using it wrong and/or maybe you don't even need it. I don't recall if I needed it on calculators I've worked on. Only for displaying numbers that are results of math calculations to control the number of digits past the decimal.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Need help with decimal point.
« Reply #11 on: July 28, 2020, 03:22:47 pm »
The calculator is finished! I put it under a new forum thread here, so people can find it better:

https://www.qb64.org/forum/index.php?topic=2867.msg121306#msg121306