JavaScript Floating Point Example

by Matthew Heironimus

HTML

<p>
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><table><tr><td><pre style="margin: 0; line-height: 125%">1
2</pre></td><td><pre style="margin: 0; line-height: 125%"><span style="color: #000080; font-weight: bold">const</span> amount: <span style="color: #000080; font-weight: bold">number</span> = <span style="color: #0000FF">8.95</span>;
<span style="color: #000080; font-weight: bold">const</span> paymentProcessorValue: <span style="color: #000080; font-weight: bold">number</span> = amount * <span style="color: #0000FF">100</span>;
</pre></td></tr></table></div>


<code>
  paymentProcessorValue = <span id="paymentProcessorValue"></span>
</code>
</p>

<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><table><tr><td><pre style="margin: 0; line-height: 125%">2</pre></td><td><pre style="margin: 0; line-height: 125%"><span style="color: #000080; font-weight: bold">const</span> paymentProcessorValue: <span style="color: #000080; font-weight: bold">number</span> = Math.round(amount * <span style="color: #0000FF">100)</span>;
</pre></td></tr></table></div>

<code>
  paymentProcessorValue = <span id="paymentProcessorValueRounded"></span>
</code>

TypeScript

const amount: number = 8.95;
const paymentProcessorValue: number = amount * 100;
const paymentProcessorValueRounded: number = Math.round(amount * 100);

$("#paymentProcessorValue").text(paymentProcessorValue);
$("#paymentProcessorValueRounded").text(paymentProcessorValueRounded);