The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.
TypeScript b2b-marketplacecomposable-commerceecommerceenterpriseheadless-commercemarketplacemedusamedusajsmulti-vendortypescript
/**
* Gets the number of decimal places in a number
* @param num - The number for which we are getting the number of decimal places
* @returns The number of decimal places
*
* @example
* getDecimalPlaces(123.456) // 3
* getDecimalPlaces(42) // 0
* getDecimalPlaces(10.0000) // 0
*/
export function getNumberOfDecimalPlaces(num: number): number {
// Convert to string and check if it contains a decimal point
const str = num.toString()
if (str.indexOf(".") === -1) {
return 0
}
// Return the length of the part after the decimal point
return str.split(".")[1].length
}