Views: 3.509 views
Staff,
Good day.
I am bringing you another useful function on a daily basis, which removes number formatting, causing “R $ 1.487.257,55” to become a string in the form of a float, that is, “1487257.55” .
For that, I created the class clsText and the function removeFormatNumber:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php class clsTexto { public static function removerFormatacaoNumero( $strNumero ) { $strNumero = trim( str_replace( "R$", null, $strNumero ) ); $vetVirgula = explode( ",", $strNumero ); if ( count( $vetVirgula ) == 1 ) { $acentos = array("."); $resultado = str_replace( $acentos, "", $strNumero ); return $resultado; } else if ( count( $vetVirgula ) != 2 ) { return $strNumero; } $strNumero = $vetVirgula[0]; $strDecimal = mb_substr( $vetVirgula[1], 0, 2 ); $acentos = array("."); $resultado = str_replace( $acentos, "", $strNumero ); $resultado = $resultado . "." . $strDecimal; return $resultado; } } ?> |
To use the function is very simple:
1 2 3 4 5 |
//Carregamos a nossa classe para a memória require_once("classes/clsTexto.php"); //Chamamos a função criada echo clsTexto::removerFormatacaoNumero("R$ 1.487.257,55"); //Vai exibir na tela 1487257.55 |
Simple, no?
First, thanks!
And if it also serves for Decimal?