Skip to content

Instantly share code, notes, and snippets.

@insanrizky
Created August 8, 2019 08:31
Show Gist options
  • Save insanrizky/20fd7a1638b3eadba8d8c0d0b6b49c45 to your computer and use it in GitHub Desktop.
Save insanrizky/20fd7a1638b3eadba8d8c0d0b6b49c45 to your computer and use it in GitHub Desktop.
Curr with K M B T
import React, { Component } from 'react';
class Curr extends Component {
state = {
successor: 0,
roundingType: '',
}
componentDidMount() {
const { value } = this.props;
this.roundingValue(value);
}
roundingValue = value => {
if (typeof value !== 'number') return '-';
const roundingTypes = ['K', 'M', 'B', 'T'];
let result = '-';
let i = 0;
let stop = false;
while (stop === false) {
const divider = 1000 ** (i + 1);
if (value >= divider) {
const successor = (value / divider).toFixed(2);
result = { successor, roundingType: roundingTypes[i] };
} else {
stop = true;
}
i++;
}
this.setState({
value: result.successor,
roundingType: result.roundingType
});
};
render() {
const { successor, roundingType } = this.state;
return (
<>
<Currency value={successor} />
{roundingType}
</>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment