Author Topic: NumberFormat - low level function  (Read 2720 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 4262
    • Oxygen Basic
NumberFormat - low level function
« on: April 06, 2012, 04:45:45 AM »
This affects the behaviour of the internal float_to_ascii function, which is used by str() etc.

It has 6 parameters but innvoking it without params restore the default behaviour.

Code: OxygenBasic
  1. 'numberformat
  2. 'action:   control how numbers are converted to strings
  3. 'use:      change the format of numbers
  4. 'example:  numberformat 3,0,0,0 : print 3.14159
  5. '          'result: 3.142
  6. 'remarks:  there are 6 parameters:
  7. '          1 decimal places (0..16)
  8. '          2 strip end zeros (0 or 1)
  9. '          3 always use scientic notation E format (0 or 1)
  10. '          4 suppress 0 before decimal point: 0.1 becomes .1 (0 or 1)
  11. '          5 insert extra leading space for non-negative numbers (0 or 1)
  12. '          6 width allocated before decimal point: (0..31)
  13. '            (inserting lead padding spaces)
  14. '
  15. '          when no parameters are given, it reverts to the default settings:
  16. '          16,1,0,0,0,0
  17. '          rounding is automatically performed before the decimal places are truncated.
  18.  
  19. cr=chr(13)+chr(10)
  20.  
  21. pr="Number Format Control Examples" cr
  22.  
  23. NumberFormat  'default
  24.  
  25. pr+=cr ">>" str 1234.5678
  26.  
  27. NumberFormat 16,1,1,0,0,0  'always use scientific notation
  28.  
  29. pr+=cr ">>" str -1234.5678
  30.  
  31. NumberFormat 08,0,0,0,0,0  '8 decimal places, leave end zeros
  32.  
  33. pr+=cr ">>" str -1234.5678
  34.  
  35. NumberFormat 16,1,0,0,1,0  'insert extra leading space for non-negative numbers
  36.  
  37. pr+=cr ">>" str 1234.5678
  38.  
  39. NumberFormat 2,1,0,0,1,10  'pad numbers 11 places before decimal point including '-' place
  40.  
  41. pr+=cr ">>" str -1234.5678
  42. pr+=cr ">>" str 1234.5678
  43.  
  44.  
  45. print pr
  46.  
  47. numberformat 'restore default seeting 16,1,0,0,0,0
  48.  
  49.  
« Last Edit: April 06, 2012, 04:52:26 AM by Charles Pegge »

Aurel

  • Guest
Re: NumberFormat - low level function
« Reply #1 on: September 01, 2013, 11:14:53 AM »
Hi Charles..
In my exprEval i receive to long (for my taste) decimal numbers..
like:
16.666667938232422
instead of something like:
16.666667
i just see this topic,so would be good to have something like SETDECIMALS n,right?
or not??
i know that i can cut signs from decimal point and then save this new string ...
so what you think?

Aurel

kryton9

  • Hero Member
  • *****
  • Posts: 721
Re: NumberFormat - low level function
« Reply #2 on: November 04, 2013, 10:22:59 PM »
Aurel, what you want you would do this according to the code I see:

Now getting: 16.666667938232422 
You    want: 16.666667

Use:
NumberFormat 6, 1



Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 4262
    • Oxygen Basic
Re: NumberFormat - low level function
« Reply #3 on: November 05, 2013, 07:36:10 AM »

str can be used with a second parameter, specifying decimal places:

single a=2/3
print a
print str(a,5)