Da stringa a float - (float) e floatval non funzionano
-
-
-
A me questo:
<?php $totale = "147,00€ (Include 26,51€ IVA)"; echo "totale=".$totale; $totale = explode ('(', $totale); echo "explode=".$totale[0]; $totale = str_replace('€','',$totale[0]); echo "<br>str_replace=".$totale; $totale = substr($totale,0,-1); echo "<br>substr=".$totale; $totale = floatval($totale); echo "<br>floatval=".$totale; ?>ritorna così:
totale=147,00€ (Include 26,51€ IVA)explode=147,00€
str_replace=147,00
substr=147,00
floatval=147cosa dovresti aspettarti invece?
-
-
-
-
-
-
Salve ottomasi, penso che il problema sia dovuto alla notazione usata nella definizione decimale perché in PHP riprende il sistema angolassone, quindi usa il punto (".") e non la virgola come da noi come separatore.
A mio avviso potrebbe essere utile convertire la virgola con il punto se non fosse possibile cambiare l'impostazione numerica.
Provi a fare questa modifica e ci faccia sapere.
-
Grazie Mentelibera!
Purtroppo non è lì il problema.
Tra i tanti tentativi che ho fatto c'era pure quello ma non ha cambiato il risultato inoltre a Bacca001 il codice funziona cosi come l'ho scritto. La stessa cosa dà a me se valorizzo a "mano" la stringa nella prima riga. -
Bacca001
Figurati, se hai bisogno ancora chiedi pure

Ora sono da chiavetta e non prende un granché e non posso usare Skype.
Se ti può aiutare posso girarti il codice completo della pagina.Le classi che chiama dovrebbero essere tutte quelle di woocommerce.
<?php /** * Print order content. Copy this file to your themes * directory /woocommerce/print-order to customize it. * * @package WooCommerce Print Invoice & Delivery Note/Templates */ if ( !defined( 'ABSPATH' ) ) exit; ?> <div class="order-branding"> <div class="company-logo"> <?php if( wcdn_get_company_logo_id() ) : ?><?php wcdn_company_logo(); ?><?php endif; ?> </div> <div class="company-info"> <?php if( !wcdn_get_company_logo_id() ) : ?><h1 class="company-name"><?php wcdn_company_name(); ?></h1><?php endif; ?> <div class="company-address"><?php wcdn_company_info(); ?></div> </div> <?php do_action( 'wcdn_after_branding', $order ); ?> </div><!-- .order-branding --> <div class="order-addresses<?php if( !wcdn_has_shipping_address( $order ) ) : ?> no-shipping-address<?php endif; ?>"> <div class="billing-address"> <h3><?php _e( 'Billing Address', 'woocommerce-delivery-notes' ); ?></h3> <address> <?php if( !$order->get_formatted_billing_address() ) _e( 'N/A', 'woocommerce-delivery-notes' ); else echo apply_filters( 'wcdn_address_billing', $order->get_formatted_billing_address(), $order ); ?> </address> </div> <div class="shipping-address"> <h3><?php _e( 'Shipping Address', 'woocommerce-delivery-notes' ); ?></h3> <address> <?php if( !$order->get_formatted_shipping_address() ) _e( 'N/A', 'woocommerce-delivery-notes' ); else echo apply_filters( 'wcdn_address_shipping', $order->get_formatted_shipping_address(), $order ); ?> </address> </div> <?php do_action( 'wcdn_after_addresses', $order ); ?> </div><!-- .order-addresses --> <div class="order-info"> <h2><?php wcdn_document_title(); ?></h2> <ul class="info-list"> <?php $fields = apply_filters( 'wcdn_order_info_fields', wcdn_get_order_info( $order ), $order ); ?> <?php foreach( $fields as $field ) : ?> <li> <strong><?php echo apply_filters( 'wcdn_order_info_name', $field['label'], $field ); ?></strong> <span><?php echo apply_filters( 'wcdn_order_info_content', $field['value'], $field ); ?></span> </li> <?php endforeach; ?> </ul> <?php do_action( 'wcdn_after_info', $order ); ?> </div><!-- .order-info --> <div class="order-items"> <table> <thead> <tr> <th class="head-name"><span><?php _e('Product', 'woocommerce-delivery-notes'); ?></span></th> <th class="head-item-price"><span><?php _e('Price', 'woocommerce-delivery-notes'); ?></span></th> <th class="head-quantity"><span><?php _e('Quantity', 'woocommerce-delivery-notes'); ?></span></th> <th class="head-price"><span><?php _e('Total', 'woocommerce-delivery-notes'); ?></span></th> </tr> </thead> <tbody> <?php if( sizeof( $order->get_items() ) > 0 ) : ?> <?php foreach( $order->get_items() as $item ) : ?> <?php $product = apply_filters( 'wcdn_order_item_product', $order->get_product_from_item( $item ), $item ); $item_meta = new WC_Order_Item_Meta( $item['item_meta'], $product ); ?> <tr> <td class="product-name"> <?php do_action( 'wcdn_order_item_before', $product, $order ); ?> <span class="name"><?php echo apply_filters( 'wcdn_order_item_name', $item['name'], $item ); ?></span> <?php $item_meta->display(); ?> <dl class="extras"> <?php if( $product && $product->exists() && $product->is_downloadable() && $order->is_download_permitted() ) : ?> <dt><?php _e( 'Download:', 'woocommerce-delivery-notes' ); ?></dt> <dd><?php printf( __( '%s Files', 'woocommerce-delivery-notes' ), count( $order->get_item_downloads( $item ) ) ); ?></dd> <?php endif; ?> <?php $fields = apply_filters( 'wcdn_order_item_fields', array(), $product, $order ); foreach ( $fields as $field ) : ?> <dt><?php echo $field['label']; ?></dt> <dd><?php echo $field['value']; ?></dd> <?php endforeach; ?> </dl> </td> <td class="product-item-price"> <span><?php echo wcdn_get_formatted_item_price( $order, $item ); ?></span> </td> <td class="product-quantity"> <span><?php echo apply_filters( 'wcdn_order_item_quantity', $item['qty'], $item ); ?></span> </td> <td class="product-price"> <span><?php echo $order->get_formatted_line_subtotal( $item ); ?></span> </td> </tr> <?php endforeach; ?> <?php endif; ?> </tbody> <tfoot> <?php if( $totals = $order->get_order_item_totals() ) : ?> <?php foreach( $totals as $total ) : ?> <?php $totale = $total['value'];?> <?php endforeach; ?> <?php $totale = explode ('(', $totale); echo $totale[0]?> <?php endif; ?> <?php if( $totals = $order->get_order_item_totals() ) : ?> <?php foreach( $totals as $total ) : ?> <tr> <td class="total-name"><span><?php echo $total['label']; ?></span></td> <td class="total-item-price"></td> <td class="total-quantity"></td> <td class="total-price"><span><?php echo $total['value']; ?></span></td> </tr> <?php endforeach; ?> <?php endif; ?> </tfoot> </table> <?php do_action( 'wcdn_after_items', $order ); ?> </div><!-- .order-items --> <div class="order-notes"> <?php if( wcdn_has_customer_notes( $order ) ) : ?> <h4><?php _e( 'Customer Note', 'woocommerce-delivery-notes' ); ?></h4> <?php wcdn_customer_notes( $order ); ?> <?php endif; ?> <?php do_action( 'wcdn_after_notes', $order ); ?> </div><!-- .order-notes --> <div class="order-thanks"> <?php wcdn_personal_notes(); ?> <?php do_action( 'wcdn_after_thanks', $order ); ?> </div><!-- .order-thanks --> <div class="order-colophon"> <div class="colophon-policies"> <?php wcdn_policies_conditions(); ?> </div> <div class="colophon-imprint"> <?php wcdn_imprint(); ?> </div> <?php do_action( 'wcdn_after_colophon', $order ); ?> </div><!-- .order-colophon --> -
-
ottomasi
Ho risolto!
In realtà quella stringa aveva al suo interno un bel po' di codice html e di conseguenza si incartava tutto!
Grazie mille per l'aiuto!Infatti te lo stavo per scrivere di fare un output encoddato in qualcosa per vedere se ci fosse altro di mezzo, meglio così

Ciao! Sembra che tu sia interessato a questa conversazione, ma non hai ancora un account.
Stanco di dover scorrere gli stessi post a ogni visita? Quando registri un account, tornerai sempre esattamente dove eri rimasto e potrai scegliere di essere avvisato delle nuove risposte (tramite email o notifica push). Potrai anche salvare segnalibri e votare i post per mostrare il tuo apprezzamento agli altri membri della comunità.
Con il tuo contributo, questo post potrebbe essere ancora migliore 💗
Registrati Accedi