Author Topic: 4 / 6 = .67  (Read 8490 times)

0 Members and 1 Guest are viewing this topic.

Offline MLambert

  • Forum Regular
  • Posts: 115
    • View Profile
4 / 6 = .67
« on: February 01, 2020, 04:34:47 am »
I am sure there is an easy way to do this without string manipulation ... how ?

I want 2 decimal not .66666666667  when I divide 2 numbers.

Thks,

Mike

Offline krovit

  • Forum Regular
  • Posts: 179
    • View Profile
Re: 4 / 6 = .67
« Reply #1 on: February 01, 2020, 05:59:26 am »
Code: QB64: [Select]
  1. ? 4/6
  2. ?
  3.  
  4. calc=4/6
  5. ? calc
  6. ?
  7.  
  8. calc#=4/6
  9. ? calc#
  10. ?
  11.  
  12. print using" #.##"; calc#
  13. ?
  14.  
  15. ? VAL(mid$(str$(calc#),instr(str$(calc#),"."),3))
  16. ?
  17.  
  18. ? val(mid$(str$(calc#+0.005),instr(str$(calc#+0.005),"."),3))
  19. ?
  20.  
  21.  
  22.  

Hello Mike,
it's a little rough and primitive but maybe it helps to understand (but maybe you already knew).

it's not a complication of QB64... is the typical system used by all programming languages.

Maybe I'm wrong but I don't think there is a direct command of the type: round-to-the-second-digit-this-number or the-result-of-this-calculation but you can do it yourself by creating a function.

Put line 18 code (appropriately modified) in a function and it all will become simple.




« Last Edit: February 01, 2020, 06:21:01 am by krovit »
Nothing is easy, especially when it appears simple (and nothing could be as dangerous as trying to do good to others)

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: 4 / 6 = .67
« Reply #2 on: February 01, 2020, 06:49:00 am »
X = INT(X * 100) / 100 to truncate at 2 decimal places.

X = INT(X * 100 + .5) / 100 to round at 2 decimal places.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline krovit

  • Forum Regular
  • Posts: 179
    • View Profile
Re: 4 / 6 = .67
« Reply #3 on: February 01, 2020, 07:07:04 am »
Yes, it's the super classic system (which I also use, of course, like everyone else)

My contribution is a complication but made in order to illustrate. In fact, I'd better keep quiet ;)

Nothing is easy, especially when it appears simple (and nothing could be as dangerous as trying to do good to others)

Offline MLambert

  • Forum Regular
  • Posts: 115
    • View Profile
Re: 4 / 6 = .67
« Reply #4 on: February 01, 2020, 09:49:07 am »
Many thks ... I haven't used masking for a long time and couldn't find/see INT.

Mike

Offline MLambert

  • Forum Regular
  • Posts: 115
    • View Profile
Re: 4 / 6 = .67
« Reply #5 on: February 01, 2020, 10:08:45 am »
Hi,

tried the INT version ... 4/3 = 133.3333 ?? both INT solutions give me the same answer

i.e.  X = INT(4 * 100 + .5) / 3  or

X = INT(4 * 100) / 3

I would like 1.33 to 2 decimal places

and 4/6 = 1.67 and this rounded

I have tried the string manipulations and it seems to work fine.

Mike

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: 4 / 6 = .67
« Reply #6 on: February 01, 2020, 10:30:03 am »
X = INT(X * 100 + .5) / 100 to round at 2 decimal places.

Seems unnecessarily complicated?  Why not the simple:
X = CINT(X * 100) / 100 to round at 2 decimal places?

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: 4 / 6 = .67
« Reply #7 on: February 01, 2020, 11:21:47 am »
Seems unnecessarily complicated?  Why not the simple:
X = CINT(X * 100) / 100 to round at 2 decimal places?

Depends on if X * 100 is an integer value, or larger.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline MLambert

  • Forum Regular
  • Posts: 115
    • View Profile
Re: 4 / 6 = .67
« Reply #8 on: February 02, 2020, 03:19:53 am »
Hi Krovit,

Using your method ... but it only returns the decimal places ... I need the whole number as well.

5/3 = .67  this is great .. but I want the 1.67.

Thks,

Mike

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: 4 / 6 = .67
« Reply #9 on: February 02, 2020, 05:32:04 am »
Code: QB64: [Select]
  1. a = 5
  2. b = 3
  3.  
  4. r = a / b
  5. c = INT(r)
  6. t = INT(100 * (r - c) + .5) / 100
  7.  
  8. PRINT c + t
You're not done when it works, you're done when it's right.

Offline MWheatley

  • Newbie
  • Posts: 64
    • View Profile
Re: 4 / 6 = .67
« Reply #10 on: February 02, 2020, 06:56:49 am »
X = INT(X * 100) / 100 to truncate at 2 decimal places.

X = INT(X * 100 + .5) / 100 to round at 2 decimal places.

This is how I've always done it, for 40+ years.  Most occasions, it's only once or twice at most in a program, ie for printing results to either screen or printer.  The idea of a function hadn't even occurred to me, because the usage is so rare.

I'd be intrigued to know if anybody has seen the value in rounding via a function, though.

Malcolm

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: 4 / 6 = .67
« Reply #11 on: February 02, 2020, 08:08:38 am »
This is way without string conversions. Function parameters are: decimal number, lenght

Code: QB64: [Select]
  1. PRINT LNUM(0.0001, 4)
  2. PRINT 22 / 7
  3. PRINT LNUM(22 / 7, 1)
  4. PRINT LNUM(22 / 7, 5)
  5.  
  6.  
  7. FUNCTION LNUM! (num!, lenght)
  8.     '1.2345, 2  =>  1.23
  9.     integernum = num \ 1
  10.     decimalnum = num / 1 - num! \ 1
  11.     n = 10 ^ lenght
  12.     newdecimalnum = decimalnum * n
  13.     newdecimalnum = INT(newdecimalnum)  'or comment this row and rewrite it as  newdecimalnum = newdecimalnum \ 1 for rounding outputs.
  14.     newdecimalnum = newdecimalnum / n
  15.     LNUM = integernum + newdecimalnum
  16.  
  17.  
« Last Edit: February 02, 2020, 08:26:31 am by Petr »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: 4 / 6 = .67
« Reply #12 on: February 02, 2020, 08:44:00 am »
Aren’t you guys just overcomplicating things?  A single line function is all anyone would ever really need...

Code: [Select]
FUNCTION RoundTo## (Num AS _FLOAT, Precision AS INTEGER)
    RoundTo = INT(Num * 10 ^ Precision + .5) / 10 ^ Precision
END FUNCTION
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: 4 / 6 = .67
« Reply #13 on: February 02, 2020, 08:57:13 am »
OK. This is the last thing I added to this forum.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: 4 / 6 = .67
« Reply #14 on: February 02, 2020, 09:14:42 am »
Hi,

tried the INT version ... 4/3 = 133.3333 ?? both INT solutions give me the same answer

i.e.  X = INT(4 * 100 + .5) / 3  or

X = INT(4 * 100) / 3

I would like 1.33 to 2 decimal places

and 4/6 = 1.67 and this rounded

I have tried the string manipulations and it seems to work fine.

Mike

 X = INT(X * 100 + .5) / 100 <— The original statement, which you misapplied here:  X = INT(4 * 100 + .5) / 3

The correct way would be either:
X = 4 / 3
X = INT( X * 100 + .5) / 100

Or:

X = INT( 4 / 3 * 100 + .5) / 100
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!