Announcement

Collapse
No announcement yet.

No rounding

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • No rounding

    Hello. I am having trouble with rounding. If after several
    caluclations, Xval = .29859932, the value rounds to just .3.

    I tried:
    Xval = round(Xval,4)
    So that I could obtain ".2986", but it wants to round to just .3.

    Can I preserve Xval without any rounding for later use in the
    program? Even it if were .298989898, I would prefer it show
    .29899 at the worst. Not just .3.

    Am I using ROUND wrong?

    Thank you.

    Robert

    ------------------

  • #2
    No way of giving you an answer without much more information. Please post a small complete program to demonstrate your issue -- then someone can help you.

    Best regards,

    Bob Zale
    PowerBASIC Inc.


    ------------------

    Comment


    • #3
      Robert,

      Try this simple test - works fine with PB32/35

      Code:
      'Testing program between
      ' - intrinsic function ROUND  - double-precision argument / result
      ' - defined function MyRound# - double-precision argument / result
      '    simple rounding function - no bankers rounding!
      
      DEFINT A-Z
      
      Xval#  = 0.29898989#
      Xval2# = Xval#                    ' value preserved for later use
      
      Xval# = ROUND(Xval#,5%)
      
      PRINT fnMyRound#(0.29898989#,5%)  ' must print 0.29899 !!!
      PRINT      ROUND(0.29898989#,5%)  ' must print 0.29899 !!!
      PRINT Xval#			  ' must print 0.29899 !!!
      PRINT Xval2#			  ' must print 0.29898989 !!!
      
      END
      
      ' from my old Turbo Basic library
      DEF fnMyRound#(Arg#, Dec%)
      ' esta função retorna o valor de arg# arredondado em dec%
       LOCAL Sinal%, Dec#, Temp#, Temp$
       LET Temp# = ABS(Arg#)
       IF Temp# >= 1d16 THEN
          fnMyRound# = Arg#
          EXIT DEF
       END IF
       LET Dec# = EXP10(Dec%)
       LET Sinal% = SGN(Arg#)
       LET Temp$ = STR$(Temp# * Dec# + .5#)
       LET Temp$ = LEFT$(Temp$, INSTR(Temp$, ".") - 1%) + "#"
       LET Temp$ = RIGHT$(Temp$, LEN(Temp$) - 1%)
       IF Sinal% < 0% THEN LET Temp$ = "-" + Temp$
       fnMyRound# = VAL(Temp$) / Dec#
      END DEF 'Round#
      edited in 11/21
      - added Xval2#
      - printout changed

      ------------------
      Arthur Gomide
      Como diria nosso profeta da bola, Dadá Maravilha:
      "Para toda Problemática existe uma Solucionática!"



      [This message has been edited by Arthur Gomide (edited November 21, 2006).]
      "The trouble with quotes on the Internet is that you can never know if they are genuine." - Abraham Lincoln.

      Comment

      Working...
      X