FIX: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "The FIX function rounds a numerical value to the next whole number closest to zero. {{PageSyntax}} : {{Parameter|result}} = FIX({{Parameter|expression}}) {{Parameters}} * {{Parameter|expression}} is any type of literal or variable numerical value or mathematical calculation. {{PageDescription}} * FIX effectively truncates (removes) the fractional part of {{Parameter|expression}}, returning the integer part. ** This means that FIX roun...")
 
No edit summary
Line 18: Line 18:
{{PageExamples}}
{{PageExamples}}
''Example 1:'' Showing the behavior of [[FIX]] with positive and negative decimal point values.
''Example 1:'' Showing the behavior of [[FIX]] with positive and negative decimal point values.
{{CodeStart}} '' ''
{{CodeStart}}
  PRINT FIX(2.5)
  PRINT FIX(2.5)
  PRINT FIX(-2.5) '' ''
  PRINT FIX(-2.5)
{{CodeEnd}}  
{{CodeEnd}}
{{OutputStart}}2  
{{OutputStart}}2
-2
-2
{{OutputEnd}}
{{OutputEnd}}
Line 28: Line 28:


''Example 2:'' The NORMAL arithmetic method (round half up) can be achieved using the function in the example code below:
''Example 2:'' The NORMAL arithmetic method (round half up) can be achieved using the function in the example code below:
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|PRINT}} MATHROUND(0.5)  
{{Cl|PRINT}} MATHROUND(0.5)
{{Cl|PRINT}} MATHROUND(1.5)
{{Cl|PRINT}} MATHROUND(1.5)
{{Cl|PRINT}} MATHROUND(2.5)
{{Cl|PRINT}} MATHROUND(2.5)
Line 38: Line 38:
{{Cl|FUNCTION}} MATHROUND(n)
{{Cl|FUNCTION}} MATHROUND(n)
     MATHROUND = {{Cl|FIX}}(n + 0.5 * {{Cl|SGN}}(n))
     MATHROUND = {{Cl|FIX}}(n + 0.5 * {{Cl|SGN}}(n))
{{Cl|END FUNCTION}} '' ''
{{Cl|END FUNCTION}}
{{CodeEnd}}  
{{CodeEnd}}
{{OutputStart}}1
{{OutputStart}}1
2
2

Revision as of 01:38, 23 January 2023

The FIX function rounds a numerical value to the next whole number closest to zero.


Syntax

result = FIX(expression)


Template:Parameters

  • expression is any type of literal or variable numerical value or mathematical calculation.


Description

  • FIX effectively truncates (removes) the fractional part of expression, returning the integer part.
    • This means that FIX rounds down for positive values and up for negative values.
  • Use INT to round down negative values. Positive values are rounded down by both.


Examples

Example 1: Showing the behavior of FIX with positive and negative decimal point values.

 PRINT FIX(2.5)
 PRINT FIX(-2.5)
2
-2


Example 2: The NORMAL arithmetic method (round half up) can be achieved using the function in the example code below:

PRINT MATHROUND(0.5)
PRINT MATHROUND(1.5)
PRINT MATHROUND(2.5)
PRINT MATHROUND(3.5)
PRINT MATHROUND(4.5)
PRINT MATHROUND(5.5)

FUNCTION MATHROUND(n)
    MATHROUND = FIX(n + 0.5 * SGN(n))
END FUNCTION
1
2
3
4
5
6


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link