How to resolve the algorithm Currency step by step in the FutureBasic programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Currency step by step in the FutureBasic programming language

Table of Contents

Problem Statement

Show how to represent currency in a simple example, using a data type that represent exact values of dollars and cents.

The IEEE 754 binary floating point representations of numbers like   2.86   and   .0765   are not exact. For this example, data will be two items with prices in dollars and cents, a quantity for each, and a tax rate. Use the values:

(That number of hamburgers is a 4 with 15 zeros after it.   The number is contrived to exclude naïve task solutions using 64 bit floating point types.)
Compute and output (show results on this page):

The tax value must be computed by rounding to the nearest whole cent and this exact value must be added to the total price before tax.
The output must show dollars and cents with a decimal point. The three results displayed should be:

Dollar signs and thousands separators are optional.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Currency step by step in the FutureBasic programming language

Source code in the futurebasic programming language

local fn Lunch_Invoice( burger_price as CFStringRef, burger_amount as CFStringRef, shake_price as CFStringRef, shake_amount as CFStringRef, tax as CFStringRef )
'~'1
DecimalNumberRef  burgerPriceDecimal = fn DecimalNumberWithString( burger_price  )
DecimalNumberRef burgerAmountDecimal = fn DecimalNumberWithString( burger_amount )
DecimalNumberRef      burgersDecimal = fn DecimalNumberByMultiplyingBy( burgerPriceDecimal, burgerAmountDecimal )
DecimalNumberRef   shakePriceDecimal = fn DecimalNumberWithString( shake_price  )
DecimalNumberRef  shakeAmountDecimal = fn DecimalNumberWithString( shake_amount )
DecimalNumberRef       shakesDecimal = fn DecimalNumberByMultiplyingBy( shakePriceDecimal, shakeAmountDecimal )
DecimalNumberRef          taxDecimal = fn DecimalNumberWithString( tax )
DecimalNumberRef     subtotalDecimal = fn DecimalNumberByAdding( burgersDecimal, shakesDecimal )
DecimalNumberRef     taxTotalDecimal = fn DecimalNumberByMultiplyingBy( subtotalDecimal, taxDecimal )
DecimalNumberRef  adjTaxTotalDecimal = fn DecimalNumberByAdding( taxTotalDecimal, fn DecimalNumberWithString( @"0.01" ) )
DecimalNumberRef    billTotalDecimal = fn DecimalNumberByAdding( subtotalDecimal, adjTaxTotalDecimal )

CFStringRef   burgersString = fn DecimalNumberString( burgersDecimal   )
CFStringRef    shakesString = fn DecimalNumberString( shakesDecimal    )
CFStringRef  taxTotalString = fn DecimalNumberString( adjTaxTotalDecimal  )
CFStringRef billTotalString = fn DecimalNumberString( billTotalDecimal )

printf @"%@", fn StringByPaddingToLength( @"", 55, @"-", 0 )
printf @"Item         Price  Quantity          Cost"
printf @"Hamburgers %6s %18s %18s", fn StringUTF8String( burger_price ), fn StringUTF8String( burger_amount ), fn StringUTF8String( burgersString )
printf @"Milkshakes %6s %18s %18s", fn StringUTF8String( shake_price ),  fn StringUTF8String( shake_amount  ), fn StringUTF8String( shakesString  )
printf @"%@", fn StringByPaddingToLength( @"", 55, @"-", 0 )
printf @"%34s %@", fn StringUTF8String( @"Subtotal:"  ), fn DecimalNumberString( subtotalDecimal  )
printf @"%35s %@", fn StringUTF8String( @"     Tax: " ), fn StringSubstringToIndex(  taxTotalString, len(taxTotalString)  - 3 )
printf @"%34s %@", fn StringUTF8String( @"   Total:"  ), fn StringSubstringToIndex( billTotalString, len(billTotalString) - 3 )
end fn

NSLog( @"%@", fn WindowPrintViewString( 1 ) )

HandleEvents

  

You may also check:How to resolve the algorithm Generic swap step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Longest common substring step by step in the Lobster programming language
You may also check:How to resolve the algorithm Rosetta Code/Rank languages by popularity step by step in the zkl programming language
You may also check:How to resolve the algorithm Detect division by zero step by step in the Java programming language
You may also check:How to resolve the algorithm Validate International Securities Identification Number step by step in the Scala programming language