Skip to content

Instantly share code, notes, and snippets.

@hanipcode
Forked from insanrizky/curr.js
Last active August 8, 2019 08:46
Show Gist options
  • Save hanipcode/f1a6b172c37187e5d41b735f14db5278 to your computer and use it in GitHub Desktop.
Save hanipcode/f1a6b172c37187e5d41b735f14db5278 to your computer and use it in GitHub Desktop.
Curr with K M B T
import React, { Component } from 'react';
const SI_SYMBOL = ["", "k", "M", "G", "T", "P", "E"];
function abbreviateNumber(number){
const tier = Math.log10(number) / 3 | 0;
if(tier == 0) return number;
const suffix = SI_SYMBOL[tier];
const scale = Math.pow(10, tier * 3);
const scaled = number / scale;
return [scaled.toFixed(1), suffix];
}
const Curr = ({ number }) => {
const [value, suffix] = abbreviateNumber(number);
return (
<>
<Currency value={value} />
{suffix}
</>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment