Skip to content

Commit

Permalink
chore: improve currency formatting (#24972)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
zlwaterfield and github-actions[bot] authored Sep 16, 2024
1 parent 4ff5be8 commit 3a578fa
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 19 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions frontend/src/lib/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,22 @@ export function humanFriendlyNumber(d: number, precision: number = DEFAULT_DECIM
return d.toLocaleString('en-US', { maximumFractionDigits: precision })
}

/** Format currency from string with commas and 2 decimal places. */
export function humanFriendlyCurrency(d: string | undefined | number): string {
if (!d) {
d = '0.00'
}

let number: number
if (typeof d === 'string') {
number = parseFloat(d)
} else {
number = d
}

return `$${number.toLocaleString('en-US', { maximumFractionDigits: 2, minimumFractionDigits: 2 })}`
}

export function humanFriendlyLargeNumber(d: number): string {
if (isNaN(d)) {
return 'NaN'
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/scenes/billing/Billing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { LemonBanner } from 'lib/lemon-ui/LemonBanner'
import { LemonLabel } from 'lib/lemon-ui/LemonLabel/LemonLabel'
import { SpinnerOverlay } from 'lib/lemon-ui/Spinner/Spinner'
import { Tooltip } from 'lib/lemon-ui/Tooltip'
import { humanFriendlyCurrency } from 'lib/utils'
import { useEffect } from 'react'
import { preflightLogic } from 'scenes/PreflightCheck/preflightLogic'
import { SceneExport } from 'scenes/sceneTypes'
Expand Down Expand Up @@ -156,7 +157,7 @@ export function Billing(): JSX.Element {
Current bill total
</LemonLabel>
<div className="font-bold text-6xl">
${billing.current_total_amount_usd_after_discount}
{humanFriendlyCurrency(billing.current_total_amount_usd_after_discount)}
</div>
{billing.discount_percent && (
<div>
Expand All @@ -180,8 +181,7 @@ export function Billing(): JSX.Element {
placement="bottom-start"
>
<strong>
$
{parseInt(billing.discount_amount_usd).toLocaleString()}
{humanFriendlyCurrency(billing.discount_amount_usd)}
</strong>
</Tooltip>{' '}
remaining credits applied to your bill.
Expand Down
30 changes: 14 additions & 16 deletions frontend/src/scenes/billing/BillingProduct.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { LemonBanner } from 'lib/lemon-ui/LemonBanner'
import { More } from 'lib/lemon-ui/LemonButton/More'
import { Tooltip } from 'lib/lemon-ui/Tooltip'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { capitalizeFirstLetter } from 'lib/utils'
import { capitalizeFirstLetter, humanFriendlyCurrency } from 'lib/utils'
import { eventUsageLogic } from 'lib/utils/eventUsageLogic'
import { useRef } from 'react'
import { getProductIcon } from 'scenes/products/Products'
Expand Down Expand Up @@ -258,14 +258,13 @@ export const BillingProduct = ({ product }: { product: BillingProductV2Type }):
>
<div className="flex flex-col items-center">
<div className="font-bold text-3xl leading-7">
$
{(
{humanFriendlyCurrency(
parseFloat(product.current_amount_usd || '0') *
(1 -
(billing?.discount_percent
? billing.discount_percent / 100
: 0))
).toFixed(2) || '0.00'}
(1 -
(billing?.discount_percent
? billing.discount_percent / 100
: 0))
)}
</div>
<span className="text-xs text-muted">
{capitalizeFirstLetter(
Expand All @@ -285,16 +284,15 @@ export const BillingProduct = ({ product }: { product: BillingProductV2Type }):
>
<div className="flex flex-col items-center justify-end">
<div className="font-bold text-muted text-lg leading-5">
$
{(
{humanFriendlyCurrency(
parseFloat(
product.projected_amount_usd || '0'
) *
(1 -
(billing?.discount_percent
? billing.discount_percent / 100
: 0))
).toFixed(2) || '0.00'}
(1 -
(billing?.discount_percent
? billing.discount_percent / 100
: 0))
)}
</div>
<span className="text-xs text-muted">Projected</span>
</div>
Expand All @@ -310,7 +308,7 @@ export const BillingProduct = ({ product }: { product: BillingProductV2Type }):
>
<div className="flex flex-col items-center">
<div className="font-bold text-3xl leading-7">
${product.current_amount_usd}
{humanFriendlyCurrency(product.current_amount_usd)}
</div>
<span className="text-xs text-muted">
per {billing?.billing_period?.interval || 'period'}
Expand Down

0 comments on commit 3a578fa

Please sign in to comment.