This chapter introduces the subroutines and functions supported by OpenOffice.org Basic that are related to numbers-including mathematical functions, conversion routines, formatting numbers as a string, and random numbers . This chapter also discusses alternate number bases.
Numerical subroutines and functions are routines that perform mathematical operations. If you use spreadsheets, you may already be familiar with mathematical functions such as Sum, which adds groups of numbers together, or even IRR, which calculates the internal rate of return of an investment. The numerical routines supported by OOo Basic (see Table 1 ) are simpler in nature, typically operating on only one or two arguments rather than an entire group of numbers.
Function | Description |
---|---|
ABS(number) | The absolute value of a specified number. |
ATN(number) | The angle, in radians, whose tangent is the specified number in the range of -Pi/2 through Pi/2. |
CByte(expression) | Round the String or numeric expression to a Byte. |
CDbl(expression) | Convert a String or numeric expression to a Double. |
CInt(expression) | Round the String or numeric expression to the nearest Integer. |
CLng(expression) | Round the String or numeric expression to the nearest Long. |
COS(number) | The cosine of the specified angle. |
CSng(expression) | Convert a String or numeric expression to a Single. |
Exp(number) | The base of natural logarithms raised to a power. |
Fix(number) | Chop off the decimal portion. |
Format(obj, format) | Fancy formatting, discussed in Chapter 6, "String Routines." |
Hex(n) | Return the hexadecimal representation of a number as a String. |
Int(number) | Round the number toward negative infinity. |
Log(number) | The logarithm of a number. In Visual Basic .NET this method can be overloaded to return either the natural (base e) logarithm or the logarithm of a specified base. |
Oct(number) | Return the octal representation of a number as a String. |
Rnd | Return a random number as a Double from 0 through 1. |
Sgn(number) | Integer value indicating the sign of a number. |
SIN(number) | The sine of an angle. |
Sqr(number) | The square root of a number. |
Str(number) | Convert a number to a String with no localization. |
TAN(number) | The tangent of an angle. |
Val(str) | Convert a String to a Double. This is very tolerant to non-numeric text. |
The mathematical functions presented in this chapter are well-known and understood by mathematicians, engineers , and others who look for excuses to use calculus in everyday life. If that is not you-if, perhaps, you do not consider the slide rule to be the coolest thing since sliced bread-don't panic when the coverage starts to become mathematical in nature. I have tried to make the information accessible while still providing the in-depth information for those who require it. The routines are topically grouped into sections so you can skip sections that you know that you won't use.
The numerical routines perform operations on numerical data. OOo Basic tries to convert arguments to an appropriate type before performing an operation. It is safer to explicitly convert data types using conversion functions, as presented in this chapter, than to rely on the default behavior. When an Integer argument is required and a floatingpoint number is provided, the default behavior is to round the number. For example, "16.8 MOD 7" rounds 16.8 to 17 before performing the operation. The Integer division operator, however, truncates the operands. For example, "Print 4 \ 0.999" truncates 0.999 to 0, causing a division-by-zero error.
Note | Table 1 contains subroutines and functions, not operators such as MOD, +, and \. Operators were covered in Chapter 2, "Language Constructs."
|