How to calculate converted value for each number in a set using a conversion rate, having its sum equal exactly a rounded fixed converted total?

23 Views Asked by At

Say I have three numeric values: a total, converted total, and a conversion rate. These are fixed, given numbers, and the two totals always have the precision of two decimal places.

total = 1084.50
conversionRate = 17.89057
convertedTotal = 1084.50 / 17.89057 = 60.618526967
convertedTotal = 60.62

Say I have $N$ numbers of which their sum equates to the variable total before conversion. These numbers will always be given, are always rounded two decimal places, and will always equal total.

n1 = 300.50
n2 = 300.00
n3 = 58.00
n4 = 103.00
n5 = 323.00

n1 + n2 + n3 + n4 + n5 = 1084.50 or total
n1+n2+...nx = total

I would like to calculate the converted value for each/any number $n_1,\cdots,n_x$ using floating point division given conversion rate, converted total, or both. However, I would like the sum of converted $n_1,\cdots,n_x$ to always equal exactly the converted total, which is rounded to two decimal places.

For below, $c$ represents that the number has been converted using the conversion rate, or ($n/$total)*convertedTotal. .XX represents that the value should be rounded to two decimal places.

cn1 = n1/conversionRate

or

cn1 = (n1/total)*convertedTotal # which I believe would always be precise enough for two decimal places

This is what I seek, only as precise as computationally possible:

cn1.XX + cn2.XX + cn3.XX + cn4.XX + cn5.XX = convertedTotal # convertedTotal is rounded two decimal places so convertedTotal.XX is also appropriate

This is definitely a question of precision. I am not sure if I should take a programming/computing data type or pure mathematical approach, or if this is a very simple question.