Peso por extenso

Estou compartilhando um trecho de código bem simples que eu escrevi para retornar um dado peso por extenso.

Minha função usa o S.I. (Sistema Internacional de Medidas), mas você pode facilmente alterar o código e adaptá-lo para outro sistema de medidas. Também é bem simples traduzi-lo para outra linguagem de programação.

function weight($weight)
{
  $weight = (float) $weight;
  $int = intval($weight);
  $dec = $weight - $int;
  $text = '';
  if ($int > 0)
  {
    $text .= $int . ' ' . ($int > 1 ? 'quilogramas' : 'quilograma');
  }
  if ($dec > 0)
  {
    if ($int > 0)
    {
      $text .= ' e ';
    }
    $dec = round($dec * 1000);
    $text .= $dec . ' ' . ($dec > 1 ? 'gramas' : 'grama');
  }
  return $text;
}

Exemplos de uso:

echo weight(20.2307);
echo weight(1);
echo weight(3);
echo weight(1.001);
Advertisements

Mac OS X and Java 1.6, keyboard input

Few months ago, after I updated (via Apple’s software update) my Java from 1.5 to 1.6, some applications started to have a strange behavior: the input by keyboard simply didn’t work as expected. Some keys, like return and shift, does work, but all letters and numbers not.

Only in the past week I’ve discovered why. The problem wasn’t with the Java version, like I thought, but with the keyboard layout. As I’m brazilian, I’m using the US International layout by Rainer Brockerhoff. Googling about this issue, I found that some custom keyboard layout doesn’t work correctly with recent Java versions.

Now I’m using the Mac’s native layout for Brazil. The bad thing is that I have to make the accentuation using option + some key. e.g., option + e to make the acute accent (´).

Opening links in new window: the XHTML way

Roger Johansson said the following about using the target attribute:

The target attribute is not allowed in strict doctypes, and since you should always use a strict doctype, the target attribute is invalid.

It’s possible to use a JavaScript trick to open a link in a new window and still having a valid XHTML Strict syntax. Continue reading “Opening links in new window: the XHTML way”