Created
August 8, 2019 08:31
-
-
Save insanrizky/20fd7a1638b3eadba8d8c0d0b6b49c45 to your computer and use it in GitHub Desktop.
Curr with K M B T
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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