jueves, 22 de noviembre de 2007

Separar Miles

Aqui van funciones para separar miles, una en javascript, java y postgres sql , siguen distintos algoritmos para llegar al mismo resultado. Aun falta definir cual es la mas optima.




JAVASCRIPT

function separar_miles(numero1) {
  var numero=""+numero1;
  var signo="";
  var entero="";
  var decimales="";
  if (numero == "" || numero == undefined) return "0.00";
  if (numero.charAt(0) == "-") //es negativo {
    numero = numero.substr(1);
    signo = "-";
  }
  if (numero.indexOf(".") >= 0) { //tiene decimales
    entero=numero.substr(0, numero.indexOf("."));
    decimales = "" + (Math.round(parseFloat(numero.substr(numero.indexOf(".")))*100)/100);
    decimales = decimales.substr(1);
    if (decimales.length == 2) decimales += "0";
  }
  else {
    entero = numero;
    decimales = ".00";
  }
  var i = entero.length;
  numero = "";
  while (1) {
    if (i - 3 >= 0)
      numero = entero.substr(i - 3, 3) + "," + numero;
    else
      numero = entero.substr(0, i) + "," + numero;
    i = i - 3;
    if (i <= 0) break;
  }
  numero= signo + numero.substr(0, numero.length-1)+decimales;
  return numero;
}



JAVA


public String separar_miles(String s, String Separador, String Decimal){
  //entrada s=1000.4 separador="," Decimal="."
  //Salida 1,000.40
  if(!s.equals(" ")){
    if (s.length() > 0){
      String sDecimal;
      String sEntero;
      if (s.indexOf('.') == -1) s += ".00"; else if((s.length() - s.indexOf('.')) == 2) s += "0";
      sDecimal = s.substring(s.indexOf('.'));
      sDecimal = Decimal + sDecimal.charAt(1) + sDecimal.charAt(2);
      sEntero = s.substring(0, s.indexOf('.'));
      s = "";
      if (sEntero.length() > 3) {
        s = sEntero.substring(0, sEntero.length() % 3);
        sEntero = sEntero.substring(sEntero.length() % 3);
        if (s.length() > 0) s += Separador;
        while (sEntero.length() > 3){
          s += sEntero.substring(0, 3) + Separador;
          sEntero = sEntero.substring(3);
        }
      }
      s+= sEntero + sDecimal;
    }
    else s = "0" + Decimal + "00";
  }
  if(s.indexOf('-')+1==s.indexOf(','))
    s="-" + s.substring(2, s.length());
  return s;
}



POSTGRES SQL


create or replace function separar_miles(_numero numeric) returns text as $$
-- numero: -123456.00 es decir numero::numeric(15,2)
declare
  _signo text;
  _decimal text;
  _entero text;
  _auxiliar text;
begin
  _signo := '';
  if _numero::numeric(15,2) < 0 then
    _signo := '-';
  end if;
  _entero := abs(_numero)::integer::text;
  _decimal := substring((abs(_numero)-abs(_numero)::integer)::numeric(15,2)::text,2);
  _auxiliar := '';
  raise notice '% % %', _signo, _entero, _decimal;
  if (length(_entero) > 3) then
    _auxiliar := _auxiliar || substring(_entero, 1, length(_entero) % 3);
    _entero := substring(_entero, (length(_entero) % 3 ) + 1 );
    if (length(_auxiliar) > 0) then
      _auxiliar := _auxiliar || ',';
    end if;
    raise notice '% % %', _signo, _entero, _decimal;
    while (length(_entero) > 3) loop
      _auxiliar := _auxiliar || substring(_entero,1,3) || ',';
      _entero := substring(_entero,4);
      raise notice '% % %', _signo, _entero, _decimal;
    end loop;
    _auxiliar := _auxiliar || _entero;
  else
    _auxiliar := _entero;
  end if;
  _signo := _signo || _auxiliar || _decimal;
 
  return _signo;
end;
$$ language plpgsql;

SELECT * FROM separar_miles(-1234567890.1);

jueves, 20 de septiembre de 2007

Encontrar el Mayor/Menor de un array

Esta funcion permite encontrar la posicion en un array de numeros(double) con el mayor/menor valor.


Funciones:

private static int dMayorArray(double[] dNumeros){
  double dMayor;
  int iPosicion=0;
  dMayor = dNumeros[0];
  iPosicion = 0;
  for (int x=1;x<dnumeros.length;x++){
    if (dNumeros[x]>dMayor){
      dMayor = dNumeros[x];
      iPosicion = x;
    }
  }
  System.out.println("El mayor es:"+Double.toString(dMayor)+" que esta en la posicion:"+Integer.toString(iPosicion));
  return iPosicion;
}



private static int dMenorArray(double[] dNumeros){
  double dMenor;
  int iPosicion=0;
  dMenor = dNumeros[0];
  iPosicion = 0;
  for (int x=1;x<dnumeros.length;x++){
    if (dNumeros[x]<dMenor){
      dMenor = dNumeros[x];
      iPosicion = x;
    }
  }
  System.out.println("El menor es:"+Double.toString(dMenor)+" que esta en la posicion:"+Integer.toString(iPosicion));
  return iPosicion;
}



Ejemplo de uso:

Anades las funciones a tu clase, luego anades esta porcion de codigo a tu main:
      double numeros[]={1.2 , 2.6, 0.5, 3.5 ,6.8, 5.2};
      int posicionMayor=dMayorArray(numeros);
      int posicionMenor=dMenorArray(numeros);

  En tu consola deberias obtener el siguiente resultado:
      El mayor es:6.8 que esta en la posicion:4
      El menor es:0.5 que esta en la posicion:2