Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void main(String[] args) {
- double number = 1234567.91011;
- char separator = ',';
- char decimal = '.';
- int maxDecimalPlaces = 3;
- int blockSize = 3;
- System.out.println(format(number, separator, decimal, maxDecimalPlaces, blockSize));
- }
- public static String format(double number, char separator, char decimal, int maxDeci, int blockSize) {
- int n = (int)number;
- char[] nStr = ("" + n).toCharArray();
- int d = (int)Math.round((number-n)*Math.pow(10, maxDeci));
- String dStr = decimal + "" + d;
- String newNStr = "";
- int numbersToStartWith = nStr.length%blockSize;
- int blocksOfNumbersAfterwards = nStr.length/blockSize;
- for (int k = 0 ; k < numbersToStartWith ; k++)
- newNStr += nStr[k];
- for (int i = 0 ; i < blocksOfNumbersAfterwards ; i++) {
- if (numbersToStartWith != 0 || i != 0)
- newNStr += separator;
- for (int j = 0 ; j < blockSize ; j++) {
- newNStr += nStr[numbersToStartWith + i*blockSize + j];
- }
- }
- return newNStr + dStr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement