Author Topic: Adding a Decimal Value  (Read 2799 times)

0 Members and 1 Guest are viewing this topic.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Adding a Decimal Value
« on: February 22, 2021, 11:58:39 am »
I want to add a decimal value to a decimal value that was converted to a whole number. The method I'm using is similar to the accompanying code

Code: QB64: [Select]
  1. a = .1234567 * 100000
  2. b = a + .25
  3. c = .25
  4. d = a + c
  5. PRINT a; b; d

The result I want to achieve is 1234567.25. The resulting number is then sent to a sorting routine after which the .25 is subtracted and the 1234567 is revered back to .1234567

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: SOLVED - Adding a Decimal Value
« Reply #1 on: February 22, 2021, 12:40:09 pm »
Sometimes I just amaze myself on how I can look at a  routine and see what I want to see and not see what is really there. My crucial variable was incorrectly dimensioned.

Offline AtomicSlaughter

  • Newbie
  • Posts: 14
    • View Profile
Re: Adding a Decimal Value
« Reply #2 on: February 25, 2021, 02:11:24 pm »
You can try this if you need to print a decimal place to screen, but you will have to convert your number into a string first using
Code: QB64: [Select]
  1. a$ = STR$(variable)
first.


Code: QB64: [Select]
  1. INPUT "enter a number", a$
  2. Numeric_Decimal (a$)
  3.  
  4. SUB Numeric_Decimal (a$)
  5.  
  6.     l = LEN(a$)
  7.     penny$ = RIGHT$(a$, 2)
  8.     IF LEN(LTRIM$(penny$)) = 1 THEN
  9.         pound$ = "0"
  10.         penny$ = "0" + LTRIM$(penny$)
  11.         conv$ = pound$ + "." + penny$
  12.         a$ = conv$
  13.     ELSE
  14.         pound$ = LEFT$(a$, (l - 2))
  15.         conv$ = pound$ + "." + penny$
  16.         a$ = conv$
  17.     END IF
  18.     IF LEN(conv$) = 3 THEN a$ = "0" + conv$
  19.