From 2464ad0aff85da577296778f780c10d5a8ceb5f4 Mon Sep 17 00:00:00 2001 From: Ammar Ahmed Date: Fri, 17 Apr 2026 15:35:33 +0500 Subject: [PATCH] mobile: fix discount calculation and price formatting Respect spacing when writing price in a currency. Fix incorrect discount calculation when comparing products of a plan based on int values, use parseFloat to parse correct values for comparision --- apps/mobile/app/hooks/use-pricing-plans.ts | 27 +++++++++++----------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/apps/mobile/app/hooks/use-pricing-plans.ts b/apps/mobile/app/hooks/use-pricing-plans.ts index 72deaf64b..1ca7a8422 100644 --- a/apps/mobile/app/hooks/use-pricing-plans.ts +++ b/apps/mobile/app/hooks/use-pricing-plans.ts @@ -276,8 +276,8 @@ const usePricingPlans = (options?: PricingPlansOptions) => { (product as Plan).period === "yearly" ? (product as Plan).price.gross : (product as Plan).period === "5-year" - ? (product as Plan).price.gross - : (product as Plan).price.gross + ? (product as Plan).price.gross + : (product as Plan).price.gross }`; } @@ -432,8 +432,8 @@ const usePricingPlans = (options?: PricingPlansOptions) => { return period.endsWith("W") ? "week" : period.endsWith("M") - ? "month" - : "year"; + ? "month" + : "year"; } else { const unit = (product as RNIap.SubscriptionIOS) ?.subscriptionPeriodUnitIOS; @@ -475,8 +475,8 @@ const usePricingPlans = (options?: PricingPlansOptions) => { type: phase.billingPeriod.endsWith("W") ? "week" : phase.billingPeriod.endsWith("M") - ? "month" - : "year" + ? "month" + : "year" }; } else { const productIos = product as RNIap.SubscriptionIOS; @@ -530,14 +530,15 @@ const usePricingPlans = (options?: PricingPlansOptions) => { const formattedPrice = numberWithCommas(monthlyPrice.toFixed(2)); return isAtLeft - ? `${symbol} ${formattedPrice}` - : `${formattedPrice} ${symbol}`; + ? `${symbol}${formattedPrice}` + : `${formattedPrice}${symbol}`; }; const getDiscountValue = (p1: string, p2: string, splitToMonth?: boolean) => { - let price1 = Platform.OS === "ios" ? parseInt(p1) : parseInt(p1) / 1000000; + let price1 = + Platform.OS === "ios" ? parseFloat(p1) : parseFloat(p1) / 1000000; const price2 = - Platform.OS === "ios" ? parseInt(p2) : parseInt(p2) / 1000000; + Platform.OS === "ios" ? parseFloat(p2) : parseFloat(p2) / 1000000; price1 = splitToMonth ? price1 / 12 : price1; @@ -587,7 +588,7 @@ const usePricingPlans = (options?: PricingPlansOptions) => { } else { priceValue = price / 1000000; } - const priceSymbol = localizedPrice.replace(/[\s\d,.]+/, ""); + const priceSymbol = localizedPrice.replace(/[\d,.]+/, ""); return { priceValue, priceSymbol, localizedPrice }; }; @@ -607,8 +608,8 @@ const usePricingPlans = (options?: PricingPlansOptions) => { (product as Plan).period === "yearly" ? ((product as Plan).price.gross / 12).toFixed(2) : (product as Plan).period === "5-year" - ? ((product as Plan).price.gross / (12 * 5)).toFixed(2) - : (product as Plan).price.gross + ? ((product as Plan).price.gross / (12 * 5)).toFixed(2) + : (product as Plan).price.gross }`; }