Skip to content

Instantly share code, notes, and snippets.

@davidgilbertson
Last active March 30, 2018 19:27
Show Gist options
  • Save davidgilbertson/a7cb41cb9b9c14e5f04b03ff7fde8ad9 to your computer and use it in GitHub Desktop.
Save davidgilbertson/a7cb41cb9b9c14e5f04b03ff7fde8ad9 to your computer and use it in GitHub Desktop.
const Price = (props) => {
const price = props.children.toLocaleString('en', {
style: props.showSymbol ? 'currency' : undefined,
currency: props.showSymbol ? 'USD' : undefined,
maximumFractionDigits: props.showDecimals ? 2 : 0,
});
return <span className={props.className}>{price}</span>
};
Price.propTypes = {
className: React.PropTypes.string,
children: React.PropTypes.number,
showDecimals: React.PropTypes.bool,
showSymbol: React.PropTypes.bool,
};
Price.defaultProps = {
children: 0,
showDecimals: true,
showSymbol: true,
};
const Page = () => {
const lambPrice = 1234.567;
const jetPrice = 999999.99;
const bootPrice = 34.567;
return (
<div>
<p>One lamb is <Price className="expensive">{lambPrice}</Price></p>
<p>One jet is <Price showDecimals={false}>{jetPrice}</Price></p>
<p>Those gumboots will set ya back <Price showDecimals={false} showSymbol={false}>{bootPrice}</Price> bucks.</p>
</div>
);
};
@hamzaerbay
Copy link

Should define the minimumFractionDigits: 0 as price props. Ow getting the error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment