Wednesday, September 7, 2011

Javascript: currency format function


function formatCurrency(num) {
   num = num.toString().replace(/\$|\,/g,'');
   if(isNaN(num))
   num = "0";
   sign = (num == (num = Math.abs(num)));
   num = Math.floor(num*100+0.50000000001);
   cents = num%100;
   num = Math.floor(num/100).toString();
   if(cents<10)
   cents = "0" + cents;
   for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
   num = num.substring(0,num.length-(4*i+3))+','+
   num.substring(num.length-(4*i+3));
   return (((sign)?'':'-') + '$' + num + '.' + cents);
}


Source:
http://javascript.internet.com/forms/currency-format.html

Java: replace chars in String

/*
        Replaces all occurrences of given character with new one
        and returns new String object.
*/

String returnString = "test string to do some change";
returnString = returnString.replace( 'o', 'd' );

Source:
http://www.javadeveloper.co.in/java-example/java-string-replace-example.html
http://javarevisited.blogspot.com/2011/12/java-string-replace-example-tutorial.html
References:
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html

Java : Using currency format

// Using currency notations i.e. Colombian Currency
public String FormateoValor(Double valor){
NumberFormat n = NumberFormat.getCurrencyInstance(new Locale("es", "CO")); 
String returnString = n.format(valor.doubleValue());
return returnString ;  
}


References:
http://www.javaworld.com/javaworld/jw-06-2001/jw-0601-cents.html
http://www.herongyang.com/JDK/Locale-java-util-Local-Localization.html

Tuesday, September 6, 2011

Java: convert String to Long and vice versa

// String to long
long l = Long.parseLong(str);

// long to string
String numCadena= String.valueOf(l);

Sources:
http://www.java-tips.org/java-se-tips/java.lang/conversion-from-string-to-long.html
http://www.java-examples.com/java-string-valueof-example

Java: convert String to int and vice versa


// string to int
int numEntero = Integer.parseInt(numCadena);

// int to String
String numCadena= String.valueOf(numEntero);

Source:
http://emilio.aesinformatica.com/2007/11/22/pasar-de-int-a-string-y-de-string-a-int-en-java/

Sunday, September 4, 2011

Python : add days, hours, minutes to datetime

newDate = datetime.datetime.now() +timedelta(days=10,hours=23,minutes=15)

Reference:
http://www.daniweb.com/software-development/python/threads/192860

Excel 2007: Switch Columns and Rows



1. Open the spreadsheet you need to change.
2. If needed, insert a blank worksheet.
3. Click the first cell of your data range such as A1.
4. Shift-click the last cell of the range. Your selection should highlight.
5. From the Edit menu, select Copy.
6. At the bottom of the page, click the tab for the blank worksheet such as Sheet2.
7. In the blank worksheet, click cell A1.
8. From the Edit menu, select Paste Special. The Paste Special dialog should appear.
9. Click the checkbox for Transpose.
10. Click OK.


Source:
http://www.timeatlas.com/5_minute_tips/general/how_to_switch_excel_columns_and_rows