You need to call the global variable to ensure that it gets the correct values.
If you add
global $woocommerce;
just before
$amount2 = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) );
that should solve your problem.