Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/utils/formatting.js
Show All 15 Lines | export const formatDate = (dateString, userLocale = 'en') => { | ||||
} | } | ||||
}; | }; | ||||
export const formatFiatBalance = (fiatBalance, optionalLocale) => { | export const formatFiatBalance = (fiatBalance, optionalLocale) => { | ||||
try { | try { | ||||
if (fiatBalance === 0) { | if (fiatBalance === 0) { | ||||
return Number(fiatBalance).toFixed(currency.cashDecimals); | return Number(fiatBalance).toFixed(currency.cashDecimals); | ||||
} | } | ||||
if (optionalLocale === undefined) { | if (typeof optionalLocale === 'undefined') { | ||||
return fiatBalance.toLocaleString({ | return fiatBalance.toLocaleString({ | ||||
maximumFractionDigits: currency.cashDecimals, | maximumFractionDigits: currency.cashDecimals, | ||||
}); | }); | ||||
} | } | ||||
return fiatBalance.toLocaleString(optionalLocale, { | return fiatBalance.toLocaleString(optionalLocale, { | ||||
maximumFractionDigits: currency.cashDecimals, | maximumFractionDigits: currency.cashDecimals, | ||||
}); | }); | ||||
} catch (err) { | } catch (err) { | ||||
return fiatBalance; | return fiatBalance; | ||||
} | } | ||||
}; | }; | ||||
export const formatSavedBalance = (swBalance, optionalLocale) => { | export const formatSavedBalance = (swBalance, optionalLocale) => { | ||||
try { | try { | ||||
if (swBalance === undefined) { | if (typeof swBalance === 'undefined') { | ||||
return 'N/A'; | return 'N/A'; | ||||
} else { | } else { | ||||
if (optionalLocale === undefined) { | if (typeof optionalLocale === 'undefined') { | ||||
return new Number(swBalance).toLocaleString({ | return new Number(swBalance).toLocaleString({ | ||||
maximumFractionDigits: currency.cashDecimals, | maximumFractionDigits: currency.cashDecimals, | ||||
}); | }); | ||||
} else { | } else { | ||||
return new Number(swBalance).toLocaleString(optionalLocale, { | return new Number(swBalance).toLocaleString(optionalLocale, { | ||||
maximumFractionDigits: currency.cashDecimals, | maximumFractionDigits: currency.cashDecimals, | ||||
}); | }); | ||||
} | } | ||||
} | } | ||||
} catch (err) { | } catch (err) { | ||||
return 'N/A'; | return 'N/A'; | ||||
} | } | ||||
}; | }; | ||||
export const formatBalance = (unformattedBalance, optionalLocale) => { | export const formatBalance = (unformattedBalance, optionalLocale) => { | ||||
try { | try { | ||||
if (optionalLocale === undefined) { | if (typeof optionalLocale === 'undefined') { | ||||
return new Number(unformattedBalance).toLocaleString({ | return new Number(unformattedBalance).toLocaleString({ | ||||
maximumFractionDigits: currency.cashDecimals, | maximumFractionDigits: currency.cashDecimals, | ||||
}); | }); | ||||
} | } | ||||
return new Number(unformattedBalance).toLocaleString(optionalLocale, { | return new Number(unformattedBalance).toLocaleString(optionalLocale, { | ||||
maximumFractionDigits: currency.cashDecimals, | maximumFractionDigits: currency.cashDecimals, | ||||
}); | }); | ||||
} catch (err) { | } catch (err) { | ||||
Show All 9 Lines | export const formatTokenBalance = ( | ||||
tokenDecimal, | tokenDecimal, | ||||
defaultLocale = 'en', | defaultLocale = 'en', | ||||
) => { | ) => { | ||||
let formattedTokenBalance; | let formattedTokenBalance; | ||||
let convertedTokenBalance; | let convertedTokenBalance; | ||||
let locale = defaultLocale; | let locale = defaultLocale; | ||||
try { | try { | ||||
if ( | if ( | ||||
tokenDecimal === undefined || | typeof tokenDecimal === 'undefined' || | ||||
unformattedBalance === undefined || | typeof unformattedBalance === 'undefined' || | ||||
typeof tokenDecimal !== 'number' || | typeof tokenDecimal !== 'number' || | ||||
!BigNumber.isBigNumber(unformattedBalance) | !BigNumber.isBigNumber(unformattedBalance) | ||||
) { | ) { | ||||
return undefined; | return undefined; | ||||
} | } | ||||
if (navigator && navigator.language) { | if (navigator && navigator.language) { | ||||
locale = navigator.language; | locale = navigator.language; | ||||
} | } | ||||
Show All 19 Lines |