How to resolve the algorithm Price fraction step by step in the D programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Price fraction step by step in the D programming language
Table of Contents
Problem Statement
A friend of mine runs a pharmacy. He has a specialized function in his Dispensary application which receives a decimal value of currency and replaces it to a standard value. This value is regulated by a government department.
Given a floating point value between 0.00 and 1.00, rescale according to the following table:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Price fraction step by step in the D programming language
Source code in the d programming language
import std.stdio, std.range;
double priceRounder(in double price) pure nothrow
in {
assert(price >= 0 && price <= 1.0);
} body {
static immutable cin = [.06, .11, .16, .21, .26, .31, .36, .41,
.46, .51, .56, .61, .66, .71, .76, .81,
.86, .91, .96, 1.01],
cout = [.10, .18, .26, .32, .38, .44, .50, .54,
.58, .62, .66, .70, .74, .78, .82, .86,
.90, .94, .98, 1.00];
return cout[cin.assumeSorted.lowerBound(price).length];
}
void main() {
foreach (const price; [0.7388727, 0.8593103, 0.826687, 0.3444635])
price.priceRounder.writeln;
}
You may also check:How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the Ursala programming language
You may also check:How to resolve the algorithm Case-sensitivity of identifiers step by step in the Ruby programming language
You may also check:How to resolve the algorithm Floyd-Warshall algorithm step by step in the jq programming language
You may also check:How to resolve the algorithm Range extraction step by step in the Oz programming language
You may also check:How to resolve the algorithm Loops/N plus one half step by step in the F# programming language