-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use suitable decimals while calculating tax #43948
Changes from all commits
4d5b18b
d03daf4
f1b4060
69e2c0c
c362173
e89431e
9443aaa
f6d8de6
2a7588d
9569e75
ead5591
21530f8
2fb7b26
6d4d7aa
6747545
f05fff3
6ea6286
db546d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,20 +87,22 @@ function convertToBackendAmount(amountAsFloat: number): number { | |
* | ||
* @note we do not support any currencies with more than two decimal places. | ||
*/ | ||
function convertToFrontendAmountAsInteger(amountAsInt: number): number { | ||
return Math.trunc(amountAsInt) / 100.0; | ||
function convertToFrontendAmountAsInteger(amountAsInt: number, currency: string = CONST.CURRENCY.USD): number { | ||
const decimals = getCurrencyDecimals(currency); | ||
return Number((Math.trunc(amountAsInt) / 100.0).toFixed(decimals)); | ||
} | ||
|
||
/** | ||
* Takes an amount in "cents" as an integer and converts it to a string amount used in the frontend. | ||
* | ||
* @note we do not support any currencies with more than two decimal places. | ||
*/ | ||
function convertToFrontendAmountAsString(amountAsInt: number | null | undefined): string { | ||
function convertToFrontendAmountAsString(amountAsInt: number | null | undefined, currency: string = CONST.CURRENCY.USD): string { | ||
if (amountAsInt === null || amountAsInt === undefined) { | ||
return ''; | ||
} | ||
return convertToFrontendAmountAsInteger(amountAsInt).toFixed(2); | ||
const decimals = getCurrencyDecimals(currency); | ||
return convertToFrontendAmountAsInteger(amountAsInt, currency).toFixed(decimals); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why we need to use toFixed(decimals) here? we just used toFixed(decimals) in convertToFrontendAmountAsInteger There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ShridharGoel How about this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is needed to convert it to a String (we can also use just the toString() method). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should use toString method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to be needed because in 2 decimals, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, make sense |
||
} | ||
|
||
/** | ||
|
@@ -111,7 +113,7 @@ function convertToFrontendAmountAsString(amountAsInt: number | null | undefined) | |
* @param currency - IOU currency | ||
*/ | ||
function convertToDisplayString(amountInCents = 0, currency: string = CONST.CURRENCY.USD): string { | ||
const convertedAmount = convertToFrontendAmountAsInteger(amountInCents); | ||
const convertedAmount = convertToFrontendAmountAsInteger(amountInCents, currency); | ||
/** | ||
* Fallback currency to USD if it empty string or undefined | ||
*/ | ||
|
@@ -137,7 +139,7 @@ function convertToDisplayString(amountInCents = 0, currency: string = CONST.CURR | |
* @param currency - IOU currency | ||
*/ | ||
function convertToShortDisplayString(amountInCents = 0, currency: string = CONST.CURRENCY.USD): string { | ||
const convertedAmount = convertToFrontendAmountAsInteger(amountInCents); | ||
const convertedAmount = convertToFrontendAmountAsInteger(amountInCents, currency); | ||
|
||
return NumberFormatUtils.format(BaseLocaleListener.getPreferredLocale(), convertedAmount, { | ||
style: 'currency', | ||
|
@@ -168,7 +170,7 @@ function convertAmountToDisplayString(amount = 0, currency: string = CONST.CURRE | |
* Acts the same as `convertAmountToDisplayString` but the result string does not contain currency | ||
*/ | ||
function convertToDisplayStringWithoutCurrency(amountInCents: number, currency: string = CONST.CURRENCY.USD) { | ||
const convertedAmount = convertToFrontendAmountAsInteger(amountInCents); | ||
const convertedAmount = convertToFrontendAmountAsInteger(amountInCents, currency); | ||
return NumberFormatUtils.formatToParts(BaseLocaleListener.getPreferredLocale(), convertedAmount, { | ||
style: 'currency', | ||
currency, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ShridharGoel
Why did we use
currency
and nottranscation.currency
?currency
seems to be the currency associated to the mileage rate or the policy:(mileageRate as MileageRate)?.currency ?? policyCurrency;
. What if the transaction is not a distance transaction and it has a different currency from the policy default?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aldo-expensify This makes sense, we can update it to use
transaction.currency
instead. Should I open a PR for this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries, I'll update it here: #43519