## Round Double To Nearest Hundredth

The following snippet demonstrates several ways to round a double in Java to the nearest integer, tenths, hundredths, or thousandths decimal place. The code handles both positive and negative numbers (and zero, too).

```double x = 0.0d;
double y = 0.0d;

// Double rounded to the nearest integer.
y = (int) (x + ((x < 0.0) ? -0.5 : 0.5));

// Double rounded to the nearest tenth.
y = ((int) ((x * 10.0) + ((x < 0.0) ? -0.5 : 0.5))) / 10.0;

// Double rounded to the nearest hundredth.
y = ((int) ((x * 100.0) + ((x < 0.0) ? -0.5 : 0.5))) / 100.0;

// Double rounded to the nearest thousandth.
y = ((int) ((x * 1000.0) + ((x < 0.0) ? -0.5 : 0.5))) / 1000.0;```

## Calculate Quarter Number In Excel

To calculate the quarter number for a given date in a regular calendar year, enter the following formula in a cell. This formula assumes a data is entered in cell A1.

``=ROUNDUP(MONTH(A1)/3,0)``

## Keypress Generator

The following is a keypress generator using the Robot class in Java. If you provide a string to the constructor and call the type() method, each of the characters in the string is generated as a keypress. The code only supports A-Z and a-z, but can be modified to cover a larger character set.

```import java.awt.Robot;
import java.awt.event.KeyEvent;

public class Typer {
private Robot robot = null;
private String characters = null;

public Typer(String characters) {
if((characters == null) || ("").equals(characters.trim())) {
characters = "";
}
this.characters = characters;

try {
robot = new Robot();
} catch(Exception e) {
}
return;
}

public String getCharacters() {
return characters;
}

public boolean type() {
if(robot == null) {
return false;
}

for(int i = 0; i < this.getCharacters().length(); i++) {
robot.keyPress(typeLetter(this.getCharacters().charAt(i)));
}
robot.keyPress(KeyEvent.VK_ENTER);
return true;
}

protected boolean delay(int delay) {
if(this.robot != null) {
robot.delay(delay);
return true;
}
return false;
}

private int typeLetter(char c) {
int keyEvent = 0;

try {
switch(c) {
case 'a':
case 'A':
keyEvent = KeyEvent.VK_A;
break;
case 'b':
case 'B':
keyEvent = KeyEvent.VK_B;
break;
case 'c':
case 'C':
keyEvent = KeyEvent.VK_C;
break;
case 'd':
case 'D':
keyEvent = KeyEvent.VK_D;
break;
case 'e':
case 'E':
keyEvent = KeyEvent.VK_E;
break;
case 'f':
case 'F':
keyEvent = KeyEvent.VK_F;
break;
case 'g':
case 'G':
keyEvent = KeyEvent.VK_G;
break;
case 'h':
case 'H':
keyEvent = KeyEvent.VK_H;
break;
case 'i':
case 'I':
keyEvent = KeyEvent.VK_I;
break;
case 'j':
case 'J':
keyEvent = KeyEvent.VK_J;
break;
case 'k':
case 'K':
keyEvent = KeyEvent.VK_K;
break;
case 'l':
case 'L':
keyEvent = KeyEvent.VK_L;
break;
case 'm':
case 'M':
keyEvent = KeyEvent.VK_M;
break;
case 'n':
case 'N':
keyEvent = KeyEvent.VK_N;
break;
case 'o':
case 'O':
keyEvent = KeyEvent.VK_O;
break;
case 'p':
case 'P':
keyEvent = KeyEvent.VK_P;
break;
case 'q':
case 'Q':
keyEvent = KeyEvent.VK_Q;
break;
case 'r':
case 'R':
keyEvent = KeyEvent.VK_R;
break;
case 's':
case 'S':
keyEvent = KeyEvent.VK_S;
break;
case 't':
case 'T':
keyEvent = KeyEvent.VK_T;
break;
case 'u':
case 'U':
keyEvent = KeyEvent.VK_U;
break;
case 'v':
case 'V':
keyEvent = KeyEvent.VK_V;
break;
case 'w':
case 'W':
keyEvent = KeyEvent.VK_W;
break;
case 'x':
case 'X':
keyEvent = KeyEvent.VK_X;
break;
case 'y':
case 'Y':
keyEvent = KeyEvent.VK_Y;
break;
case 'z':
case 'Z':
keyEvent = KeyEvent.VK_Z;
break;
}
} catch(Exception e) {
}
return keyEvent;
}
}```