Skip to content

Instantly share code, notes, and snippets.

@AshleyGrant
Last active March 22, 2018 23:12
Show Gist options
  • Save AshleyGrant/a3066bfbc1b81561223d5314e81aa3ea to your computer and use it in GitHub Desktop.
Save AshleyGrant/a3066bfbc1b81561223d5314e81aa3ea to your computer and use it in GitHub Desktop.
Decimal Math Stuff
<template>
<require from="money-round"></require>
<div>
<input type="number" value.bind="a" />
</div>
<div>
<input type="number" value.bind="b" />
</div>
<div>
My result = ${ a * b | moneyRound }
</div>
<div>
Math result = ${ a * b }
</div>
</template>
import {computedFrom} from 'aurelia-framework';
export class App {
a = "1111.11";
b = "0.25";
@computedFrom('a', 'b')
get resultOne() {
return multiply(this.a, this.b);
}
}
function multiply(a, b) {
if(a === undefined || b === undefined) {
throw new 'invalid numbers';
}
const [aChar, aMant] = a.replace(',', '')
.split('.')
.map( x => ({ number: parseInt(x), text: x}));
const [bChar, bMant] = b.replace(',', '')
.split('.')
.map( x => ({ number: parseInt(x), text: x}));
const aAsInt = parseInt(a.replace(',', '')
.replace('.', ''));
const bAsInt = parseInt(b.replace(',', '')
.replace('.', ''));
let decimalCount = 0;
if(aMant) {
decimalCount += aMant.text.length;
}
if(bMant) {
decimalCount += bMant.text.length;
}
const intResult = (aAsInt * bAsInt).toString();
if( decimalCount > 0 ) {
let decimalPortionAsInt = parseInt(intResult.slice(decimalCount * -1));
let decimalPortion = intResult.slice(decimalCount * -1);
if (decimalCount === 1) {
decimalPortion = `${decimalPortion}0`;
} else if( decimalCount === 3 ) {
if( decimalPortion[2] === '5' ) {
decimalPortion = (parseInt(decimalPortion.slice(0,2))+1).toString();
} else {
}
} else if (decimalCount > 3 ) {
decimalPortion = `${Math.round(parseFloat(`${decimalPortion.slice(0,2)}.${decimalPortion.slice(2)}`))}`;
}
return `${intResult.slice(0, intResult.length - decimalCount)}.${decimalPortion}`;
}
return `${intResult.toString()}.00`;
}
<!doctype html>
<html>
<head>
<title>Aurelia</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body aurelia-app>
<h1>Loading...</h1>
<script src="https://cdn.rawgit.com/jdanyow/aurelia-bundle/v1.0.3/jspm_packages/system.js"></script>
<script src="https://cdn.rawgit.com/jdanyow/aurelia-bundle/v1.0.3/config.js"></script>
<script>
System.import('aurelia-bootstrapper');
</script>
</body>
</html>
export class MoneyRoundValueConverter {
toView(value) {
return decimalRound(value, -2)
}
}
function decimalRound(inputValue, inputExp) {
// If the exp is undefined or zero...
if (typeof inputExp === 'undefined' || +inputExp === 0) {
return Math.round(value);
}
let value = +inputValue;
let exp = +inputExp;
// If the value is not a number or the exp is not an integer...
if (value === null || isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
return NaN;
}
const isNegative = value < 0;
if(isNegative) {
value *= -1;
}
// Shift
value = value.toString().split('e');
value = Math.round(+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));
// Shift back
value = value.toString().split('e');
const roundedValue = +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
return isNegative ? roundedValue * -1 : roundedValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment