Ver Mensaje Individual
  #1 (permalink)  
Antiguo 03/05/2010, 08:42
maxwellxp
 
Fecha de Ingreso: abril-2010
Mensajes: 143
Antigüedad: 14 años
Puntos: 0
misterioso problema de redondeo

todos buscan el redondeo de decimales, yo no.
Código PHP:
Ver original
  1. <?php
  2. function exp_to_dec($float_str)
  3. // formats a floating point number string in decimal notation, supports signed floats, also supports non-standard formatting e.g. 0.2e+2 for 20
  4. // e.g. '1.6E+6' to '1600000', '-4.566e-12' to '-0.000000000004566', '+34e+10' to '340000000000'
  5. // Author: Bob
  6. {
  7.     // make sure its a standard php float string (i.e. change 0.2e+2 to 20)
  8.     // php will automatically format floats decimally if they are within a certain range
  9.     $float_str = (string)((float)($float_str));
  10.  
  11.     // if there is an E in the float string
  12.     if(($pos = strpos(strtolower($float_str), 'e')) !== false)
  13.     {
  14.         // get either side of the E, e.g. 1.6E+6 => exp E+6, num 1.6
  15.         $exp = substr($float_str, $pos+1);
  16.         $num = substr($float_str, 0, $pos);
  17.        
  18.         // strip off num sign, if there is one, and leave it off if its + (not required)
  19.         if((($num_sign = $num[0]) === '+') || ($num_sign === '-')) $num = substr($num, 1);
  20.         else $num_sign = '';
  21.         if($num_sign === '+') $num_sign = '';
  22.        
  23.         // strip off exponential sign ('+' or '-' as in 'E+6') if there is one, otherwise throw error, e.g. E+6 => '+'
  24.         if((($exp_sign = $exp[0]) === '+') || ($exp_sign === '-')) $exp = substr($exp, 1);
  25.         else trigger_error("Could not convert exponential notation to decimal notation: invalid float string '$float_str'", E_USER_ERROR);
  26.        
  27.         // get the number of decimal places to the right of the decimal point (or 0 if there is no dec point), e.g., 1.6 => 1
  28.         $right_dec_places = (($dec_pos = strpos($num, '.')) === false) ? 0 : strlen(substr($num, $dec_pos+1));
  29.         // get the number of decimal places to the left of the decimal point (or the length of the entire num if there is no dec point), e.g. 1.6 => 1
  30.         $left_dec_places = ($dec_pos === false) ? strlen($num) : strlen(substr($num, 0, $dec_pos));
  31.        
  32.         // work out number of zeros from exp, exp sign and dec places, e.g. exp 6, exp sign +, dec places 1 => num zeros 5
  33.         if($exp_sign === '+') $num_zeros = $exp - $right_dec_places;
  34.         else $num_zeros = $exp - $left_dec_places;
  35.        
  36.         // build a string with $num_zeros zeros, e.g. '0' 5 times => '00000'
  37.         $zeros = str_pad('', $num_zeros, '0');
  38.        
  39.         // strip decimal from num, e.g. 1.6 => 16
  40.         if($dec_pos !== false) $num = str_replace('.', '', $num);
  41.        
  42.         // if positive exponent, return like 1600000
  43.         if($exp_sign === '+') return $num_sign.$num.$zeros;
  44.         // if negative exponent, return like 0.0000016
  45.         else return $num_sign.'0.'.$zeros.$num;
  46.     }
  47.     // otherwise, assume already in decimal notation and return
  48.     else return $float_str;
  49. }
  50. ?>
  51. <?php
  52. if (isset($_POST['valor']))
  53. {
  54. $en_menos_con_seg_ob = $_POST['en_menos_con_seg_ob'] * 3.80;
  55. }
  56. ?>
  57. <style type="text/css">
  58. <!--
  59. .Estilo5 {font-size: 12px}
  60. -->
  61. </style>
  62. <form name="form1" method="post" action="">
  63.   <table width="90" height="41" border="0" cellpadding="0" cellspacing="0">
  64.     <tr>
  65.       <td><div align="center" class="Estilo5">3.80</div>
  66.           <label>
  67.           <input name="en_menos_con_seg_ob" type="text" id="en_menos_con_seg_ob" style="text-align:center" size="10"/>
  68.         </label></td>
  69.     </tr>
  70.   </table>
  71.   <table width="146" border="0" cellpadding="0" cellspacing="0">
  72.     <tr>
  73.       <td width="176" height="48"><div align="center">
  74.           <?php if(isset($valor)) { echo exp_to_dec($en_menos_con_seg_ob); } ?>
  75.       </div></td>
  76.     </tr>
  77.   </table>
  78.   <p>
  79.     <label>
  80.     <input type="submit" name="valor" id="valor" value="Enviar">
  81.     </label>
  82.   </p>
  83. </form>
eso es un extracto de mi planilla...
lo mas raro, q al poner 999.65 en el textbox, me devuelve 3798.67. Eso esta perfecto, pero en la planilla entera me devuelve 3798.7, osea redondea. No quiero que redondee!