setAmount( self::amount_to_cents( $amount, $currency ) ); $amount_money->setCurrency( $currency ); return $amount_money; } /** * Converts a float amount to cents. * * @since 2.0.0 * * @param float $amount float amount to convert * @param string $currency currency code for the amount * @return int */ public static function amount_to_cents( $amount, $currency = '' ) { if ( ! $currency ) { $currency = get_woocommerce_currency(); } $cents_factor = 10 ** self::get_currency_decimals( $currency ); return (int) ( round( $cents_factor * $amount ) ); } /** * Converts an amount in cents to a float. * * @since 2.0.0 * * @param int $cents amount in cents * @param string $currency currency code for the amount * @return float */ public static function cents_to_float( $cents, $currency = '' ) { if ( ! $currency ) { $currency = get_woocommerce_currency(); } $cents_factor = 10 ** self::get_currency_decimals( $currency ); return (float) ( $cents / $cents_factor ); } /** * Gets the standard number of decimals for the given currency. * * @since 2.0.2 * * @param string $currency currency code * @return int */ public static function get_currency_decimals( $currency ) { $other_currencies = array( 'BIF' => 0, 'CLP' => 0, 'DJF' => 0, 'GNF' => 0, 'HUF' => 0, 'JPY' => 0, 'KMF' => 0, 'KRW' => 0, 'MGA' => 0, 'PYG' => 0, 'RWF' => 0, 'VND' => 0, 'VUV' => 0, 'XAF' => 0, 'XOF' => 0, 'XPF' => 0, ); $locale_info = include WC()->plugin_path() . '/i18n/locale-info.php'; $currencies = wp_list_pluck( $locale_info, 'num_decimals', 'currency_code' ); // ensure the values set in local-info.php always override the above $currencies = array_merge( $other_currencies, $currencies ); /** * Filters the number of decimals to use for a given currency when converting to or from its smallest denomination. * * @since 2.0.2 * * @param int $decimals number of decimals * @param string $currency currency code */ return apply_filters( 'wc_square_currency_decimals', isset( $currencies[ $currency ] ) ? $currencies[ $currency ] : 2, $currency ); } }