Repository navigation
Modern Finance DecimalApportion
Paul Squires edited this page Oct 19, 2026
·
5 revisions
Home › Finance › Finance › DecimalApportion
Splits a Decimal total by weights, at a scale you choose, into parts that add up exactly.
DecimalApportion( total, scale, weights( ), result( ) )#import "std/money"
using money
'' Splitting 100.00 three ways gives 33.33 each, whose sum is 99.99.
'' Every accounting system has to decide where the missing cent goes, and
'' doing it ad hoc is how a reconciliation fails. Largest-remainder: THE
'' PARTS ALWAYS SUM TO THE TOTAL, EXACTLY
dim as int64 w( 0 to 2 ) = { 1, 1, 1 }
redim as Decimal r( 0 )
DecimalApportion( DecimalParse( "100.00" ).Value( ), 2, w( ), r( ) )
print r( 0 ).ToString( ) '' 33.34
print r( 1 ).ToString( ) '' 33.33
print r( 2 ).ToString( ) '' 33.33
print ( r( 0 ) + r( 1 ) + r( 2 ) ).ToString( ) '' 100.00
'' Weights need not be equal, and the scale is yours to pick -- which is
'' the one thing currency.tiko's Apportion could not offer, having only
'' four decimal places to apportion at
dim as int64 w2( 0 to 2 ) = { 1, 2, 3 }
DecimalApportion( DecimalParse( "100.00" ).Value( ), 2, w2( ), r( ) )
print r( 0 ).ToString( ); " "; r( 1 ).ToString( ); " "; r( 2 ).ToString( )
'' 16.67 33.33 50.00Library: Finance
Category: Finance
Documented under: Nothing Becomes A Keyword
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing