Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Floating point round off

Source: Ruby: Floating point round off [hans-eric.com]

With a little monkey patching we can add custom round off methods to the Float class.

   1  
   2  class Float
   3    def round_to(x)
   4      (self * 10**x).round.to_f / 10**x
   5    end
   6  
   7    def ceil_to(x)
   8      (self * 10**x).ceil.to_f / 10**x
   9    end
  10  
  11    def floor_to(x)
  12      (self * 10**x).floor.to_f / 10**x
  13    end
  14  end

Example usage:

num = 138.249
num.round_to(2)
# => 138.25

num.floor_to(2)
# => 138.24

num.round_to(-1)
# => 140.0



PHP : Hace Script / Ago Script

Hace Script / Ago Script
if we call with a timestamp of a month ago, he print "Hace 1 mes". If you want this in english should traduction. Sorry my english

   1  
   2  function hace($timestamp)
   3  {
   4  $diferencia = time() - $timestamp;
   5  	if($diferencia > 0)
   6  	{
   7  $periodo = array("segundo", "minuto", "hora", "dia", "semana", "mes" , "año", "decada");
   8  $longitud = array(           "60"    , "60"  , "24" , "7"     , "4.35", "12"       , "10"    );
   9  	
  10  	for($j = 0; $diferencia >= $longitud[$j]; $j++)
  11  	$diferencia /= $longitud[$j];
  12  	
  13  	$diferencia = round($diferencia);
  14  	
  15  	if($diferencia != 1)
  16  	{
  17  		if($periodo[$j] == "mes")
  18  		$periodo[$j].= "es";
  19  		else
  20  		$periodo[$j].= "s";
  21  	}
  22  	
  23  	return "Hace <b>".$diferencia."</b> ".$periodo[$j];
  24  	}
  25  }

Draw round corner rectangle

   1  
   2          public void DrawRoundRect(Graphics g, Pen p, float X, float Y, float width, float height, float radius)
   3          {
   4              GraphicsPath gp = new GraphicsPath();
   5  
   6              gp.AddLine(X + radius, Y, X + width - (radius * 2), Y);
   7              gp.AddArc(X + width - (radius * 2), Y, radius * 2, radius * 2, 270, 90);
   8              gp.AddLine(X + width, Y + radius, X + width, Y + height - (radius * 2));
   9              gp.AddArc(X + width - (radius * 2), Y + height - (radius * 2), radius * 2, radius * 2, 0, 90);
  10              gp.AddLine(X + width - (radius * 2), Y + height, X + radius, Y + height);
  11              gp.AddArc(X, Y + height - (radius * 2), radius * 2, radius * 2, 90, 90);
  12              gp.AddLine(X, Y + height - (radius * 2), X, Y + radius);
  13              gp.AddArc(X, Y, radius * 2, radius * 2, 180, 90);
  14              gp.CloseFigure();
  15  
  16              g.DrawPath(p, gp);
  17              gp.Dispose();
  18          }

JS Round Number

// rounds a number and returns with 2 decimals only
   1  
   2  function roundNumber(x)
   3  	{
   4  		x = x.toFixed(2);
   5  		x = x.replace(/\./,'.');
   6  		return x;
   7  	}
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS