Skip to content

Commit

Permalink
💵 Fix repeat and format currency
Browse files Browse the repository at this point in the history
  • Loading branch information
iantrich committed Jan 11, 2019
1 parent f3b48c4 commit 7b98db3
Showing 1 changed file with 36 additions and 11 deletions.
47 changes: 36 additions & 11 deletions pc-card.js
Expand Up @@ -2,7 +2,6 @@ import {
LitElement,
html
} from "https://unpkg.com/@polymer/lit-element@latest/lit-element.js?module";
import { repeat } from "https://unpkg.com/lit-html@0.9.0/lib/repeat.js?module";

class PcCard extends LitElement {
static get properties() {
Expand Down Expand Up @@ -46,26 +45,52 @@ class PcCard extends LitElement {
? html`
<paper-item>
${pc_networth.attributes.friendly_name.replace("PC ", "")}:
${pc_networth.state}
${this.formatMoney(pc_networth.state)}
</paper-item>
`
: ""
}
${
repeat(
pc_entities,
(item, index) =>
html`
<paper-item>
${item.attributes.friendly_name.replace("PC ", "")}:
${item.state}
</paper-item>
`
pc_entities.map(
entity => html`
<paper-item>
${entity.attributes.friendly_name.replace("PC ", "")}:
${this.formatMoney(entity.state)}
</paper-item>
`
)
}
</ha-card>
`;
}

formatMoney(amount, decimalCount = 2, decimal = ".", thousands = ",") {
try {
decimalCount = Math.abs(decimalCount);
decimalCount = isNaN(decimalCount) ? 2 : decimalCount;

const negativeSign = amount < 0 ? "-" : "";

let i = parseInt(
(amount = Math.abs(Number(amount) || 0).toFixed(decimalCount))
).toString();
let j = i.length > 3 ? i.length % 3 : 0;

return (
negativeSign +
(j ? i.substr(0, j) + thousands : "") +
i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands) +
(decimalCount
? decimal +
Math.abs(amount - i)
.toFixed(decimalCount)
.slice(2)
: "")
);
} catch (e) {
console.log(e);
}
}
}

customElements.define("pc-card", PcCard);

0 comments on commit 7b98db3

Please sign in to comment.