Replace Line Feeds With Commas Using Javascript

The following JavaScript function can be used to replace line feeds and carriage returns with commas. I find this particularly useful when formatting a data column from Excel into an SQL constraint.

Sample Input

1
2
3
4
5

Sample Result

1,2,3,4,5

The JavaScript function uses a simple regular expression to remove line feeds and carriage returns. The comma as the second argument to the ref.value.replace function may be replaced with any other character to meet your specific requirement.

Live Test

Test the code using the following text area. Enter a carriage return / line feed separated column of data, move focus off the text area, and the code will automatically format the list.

Source Code

function remove_crlf(ref) {
  ref.value = ref.value.replace(new RegExp("[\r\n]", "gm"), ",");
}

Using a simple HTML page with a Text Area, we can paste the data list into the form. When the cursor is moved away from the Text Area, the function is automatically called and the list is formatted with commas replacing the line feeds and carriage returns on each line.

Putting all of the code together, we get the following which can be saved to an html file.

<html>
  <head>
    <script language='javascript'>
      function remove_crlf(ref) {
        ref.value = ref.value.replace(new RegExp("[\r\n]", "gm"), ",");
      }
    </script>
  </head>
  <body>
    <form name='frmremover'>
      <textarea name="txtdata" rows="5" cols="40" onblur="javascript:remove_crlf(this)"></textarea>
    </form>
  </body>
</html>

How to Round Double to Any Decimal Place in Java

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

If you don’t read through any of the following detail, be sure to use the BigDecimal rounding method (Method 4). The other methods have precision issues which don’t provide a reliable way to round decimals especially if you’re performing calculations to put someone on the Moon.

public static String roundDecimalUsingBigDecimal(String strValue, int decimalPlace) {
  return new BigDecimal(strValue).setScale(decimalPlace, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString();
}

Why is Rounding So Complicated? Round-off Errors!

It has been awhile, but I’m fairly certain that round-off errors were part of the first lesson in Computer Science 101. As a generalization, computers are limited in the way numbers are represented in binary. This isn’t a limitation of Java or any other programming language specifically, but it’s a limitation of how fractional numbers can be represented in a finite binary storage model. As a simple example, the fraction 1/3 cannot be accurately represented in binary since it is a non-terminating expansion, e.g. 0.333…, and floating point primitives have a defined maximum storage size. Certain other fractions may not represented without adding additional bits to the field for increased accuracy. If additional bits are not available, there will be small errors in the represented numbers.

The following example demonstrates round-off errors using the input value 255.335.

public class RoundOffErrorExample {
  public static void main(String[] args) {
    double dValue1 = 255.335d;
    double dValue2 = 0.1d;
    double dValue3 = 10.0d;
    double dValue4 = (dValue1 / dValue2) / dValue3;
    System.out.println("dValue1 = " + dValue1);
    System.out.println("dValue2 = " + dValue2);
    System.out.println("dValue3 = " + dValue3);
    System.out.println("dValue4 = " + dValue4);
  }
}

The expected value after some trivial calculations is 255.335. However, the actual result is 255.33499999999998. If the input value 255.335 is rounded to the hundredths decimal place, the expected result is 255.34. If the actual result is rounded to the hundredths decimal place, the result is 255.33.

dValue1 = 255.335
dValue2 = 0.1
dValue3 = 10.0
dValue4 = 255.33499999999998

It may not seem like a big issue if some numbers have small errors in outer decimal places, but these errors accumulate and magnify as further calculations are performed. Continuing with the 255.335 example, assume the rounding error occurred in a banking application across millions of transactions. These one-cent errors could accumulate into a material financial impact over time. This has been part of the plotline in several movies. In Superman III, the character portrayed by Richard Pryor programmed a way to capture (embezzle) these round-off errors into a separate account. This is called salami slicing or penny shaving.

How to Handle Decimal Rounding in Java

The source code I’ve provided below has four possible methods to round decimal numbers in Java. I’ll provide a walkthrough of each method to show the underlying issues. Again, the summary is to use BigDecimal to manipulate fractional numbers to get the most reliable results from calculations. For each described method, I’ll use two input values to demonstrate the issues: 1.335 and the first 50 decimal digits of pi.

Java Options to Round Decimals - Example Using 3 Decimal Digits
Java Options to Round Decimals – Example Using 3 Decimal Digits
Java Options to Round Decimals - Example Using 50 Decimal Digits of Pi
Java Options to Round Decimals – Example Using 50 Decimal Digits of Pi

Method 1: Using Math.round() [Result – Fail]

I’m including Math.round() to illustrate that this method only rounds to the nearest whole number. Since it doesn’t provide any capability to round the fractional part of the number, the Math.round() method isn’t useful for rounding decimals.

Using 1.335 as the input, the result is 1.0.

Method 1:  Rounded using Math.round()
--------
Rounded value 1.335 using Math.round(): 1

Using the first 50 decimal digits of pi, the result is 3.0.

Method 1:  Rounded using Math.round()
--------
Rounded value 3.141592653589793 using Math.round(): 3

Method 2: Using Powers of 10 [Result – Fail]

This method uses a combination of type conversion and multiplication/division by powers of 10. While this initially appears to be a reliable calculation, the combination of type conversion and round-off errors in the underlying primitives results in calculation errors for certain input values. This method also fails when the multiplication by powers of 10 exceeds the maximum value of what a double or long can store.

Using the input value 1.335 and rounding to the 2nd decimal place (hundredths), the math is 1.335 * 100 = 133.5, 133.5 + 0.5 = 134; 134 / 100 = 1.34. If we wanted to round to the 3rd decimal place (thousandths), 1000 would be used instead of 100. In this instance, the rounding works as expected. However, with certain input value, I have seen this method fail as well.

Method 2:  Rounded using Powers of 10
--------
Rounded value 1.335 to 0 decimal places: 1.0
Rounded value 1.335 to 1 decimal places: 1.3
Rounded value 1.335 to 2 decimal places: 1.34
Rounded value 1.335 to 3 decimal places: 1.335

Using the first 50 decimal digits of pi, we find this method fails when rounded to the 19th decimal place. Also, the double used to store the value does not contain all 50 decimal digits. Depending on the size on the input value, this method begins to fail when the interim value multiplied by powers of 10 exceeds the maximum or minimum value of the underlying primitive data type (either double or long). The output illustrates utter failure at the 19th and 20th decimal places. The output also demonstrates limitations on the number of decimal places and precision loss at 15 decimal places.

Method 2:  Rounded using Powers of 10
--------
Rounded value 3.141592653589793 to 0 decimal places: 3.0
Rounded value 3.141592653589793 to 1 decimal places: 3.1
Rounded value 3.141592653589793 to 2 decimal places: 3.14
Rounded value 3.141592653589793 to 3 decimal places: 3.142
Rounded value 3.141592653589793 to 4 decimal places: 3.1416
Rounded value 3.141592653589793 to 5 decimal places: 3.14159
Rounded value 3.141592653589793 to 6 decimal places: 3.141593
Rounded value 3.141592653589793 to 7 decimal places: 3.1415927
Rounded value 3.141592653589793 to 8 decimal places: 3.14159265
Rounded value 3.141592653589793 to 9 decimal places: 3.141592654
Rounded value 3.141592653589793 to 10 decimal places: 3.1415926536
Rounded value 3.141592653589793 to 11 decimal places: 3.14159265359
Rounded value 3.141592653589793 to 12 decimal places: 3.14159265359
Rounded value 3.141592653589793 to 13 decimal places: 3.1415926535898
Rounded value 3.141592653589793 to 14 decimal places: 3.14159265358979
Rounded value 3.141592653589793 to 15 decimal places: 3.141592653589793
Rounded value 3.141592653589793 to 16 decimal places: 3.141592653589793
Rounded value 3.141592653589793 to 17 decimal places: 3.141592653589793
Rounded value 3.141592653589793 to 18 decimal places: 3.141592653589793
Rounded value 3.141592653589793 to 19 decimal places: 0.9223372036854776
Rounded value 3.141592653589793 to 20 decimal places: 0.09223372036854775

Method 3: Using DecimalFormat [Result – Fail]

This method also suffers from underlying round-off errors resulting in a miscalculation at the second decimal place with an input value of 1.335. The expected result is 1.34, however, the actual result is 1.33.

Method 3:  Rounded using DecimalFormat
--------
Rounded value 1.335 to 0 decimal places: 1
Rounded value 1.335 to 1 decimal places: 1.3
Rounded value 1.335 to 2 decimal places: 1.33
Rounded value 1.335 to 3 decimal places: 1.335

Using pi, we find this method fails at the 16th decimal place. Precision that was included in the input value is now lost after the 15th decimal place in the output.

Method 3:  Rounded using DecimalFormat
--------
Rounded value 3.141592653589793 to 0 decimal places: 3
Rounded value 3.141592653589793 to 1 decimal places: 3.1
Rounded value 3.141592653589793 to 2 decimal places: 3.14
Rounded value 3.141592653589793 to 3 decimal places: 3.142
Rounded value 3.141592653589793 to 4 decimal places: 3.1416
Rounded value 3.141592653589793 to 5 decimal places: 3.14159
Rounded value 3.141592653589793 to 6 decimal places: 3.141593
Rounded value 3.141592653589793 to 7 decimal places: 3.1415927
Rounded value 3.141592653589793 to 8 decimal places: 3.14159265
Rounded value 3.141592653589793 to 9 decimal places: 3.141592654
Rounded value 3.141592653589793 to 10 decimal places: 3.1415926536
Rounded value 3.141592653589793 to 11 decimal places: 3.14159265359
Rounded value 3.141592653589793 to 12 decimal places: 3.14159265359
Rounded value 3.141592653589793 to 13 decimal places: 3.1415926535898
Rounded value 3.141592653589793 to 14 decimal places: 3.14159265358979
Rounded value 3.141592653589793 to 15 decimal places: 3.141592653589793
Rounded value 3.141592653589793 to 16 decimal places: 3.141592653589793
Rounded value 3.141592653589793 to 17 decimal places: 3.141592653589793
Rounded value 3.141592653589793 to 18 decimal places: 3.141592653589793
Rounded value 3.141592653589793 to 19 decimal places: 3.141592653589793
Rounded value 3.141592653589793 to 20 decimal places: 3.141592653589793

Method 4: Using BigDecimal [Result – Success]

This is the recommended solution for rounding in Java. BigDecimal can handle both very large and very small numbers without round-off errors and without losing precision and it is generally limited only by available memory.

Method 4:  Rounded using BigDecimal
--------
Rounded value 1.335 to 0 decimal places: 1
Rounded value 1.335 to 1 decimal places: 1.3
Rounded value 1.335 to 2 decimal places: 1.34
Rounded value 1.335 to 3 decimal places: 1.335

Rounding 50 decimal digits of pi results in the expected results for all cases.

Method 4:  Rounded using BigDecimal
--------
Rounded value 3.14159265358979323846264338327950288419716939937510 to 0 decimal places: 3
Rounded value 3.14159265358979323846264338327950288419716939937510 to 1 decimal places: 3.1
Rounded value 3.14159265358979323846264338327950288419716939937510 to 2 decimal places: 3.14
Rounded value 3.14159265358979323846264338327950288419716939937510 to 3 decimal places: 3.142
Rounded value 3.14159265358979323846264338327950288419716939937510 to 4 decimal places: 3.1416
Rounded value 3.14159265358979323846264338327950288419716939937510 to 5 decimal places: 3.14159
Rounded value 3.14159265358979323846264338327950288419716939937510 to 6 decimal places: 3.141593
Rounded value 3.14159265358979323846264338327950288419716939937510 to 7 decimal places: 3.1415927
Rounded value 3.14159265358979323846264338327950288419716939937510 to 8 decimal places: 3.14159265
Rounded value 3.14159265358979323846264338327950288419716939937510 to 9 decimal places: 3.141592654
Rounded value 3.14159265358979323846264338327950288419716939937510 to 10 decimal places: 3.1415926536
Rounded value 3.14159265358979323846264338327950288419716939937510 to 11 decimal places: 3.14159265359
Rounded value 3.14159265358979323846264338327950288419716939937510 to 12 decimal places: 3.14159265359
Rounded value 3.14159265358979323846264338327950288419716939937510 to 13 decimal places: 3.1415926535898
Rounded value 3.14159265358979323846264338327950288419716939937510 to 14 decimal places: 3.14159265358979
Rounded value 3.14159265358979323846264338327950288419716939937510 to 15 decimal places: 3.141592653589793
Rounded value 3.14159265358979323846264338327950288419716939937510 to 16 decimal places: 3.1415926535897932
Rounded value 3.14159265358979323846264338327950288419716939937510 to 17 decimal places: 3.14159265358979324
Rounded value 3.14159265358979323846264338327950288419716939937510 to 18 decimal places: 3.141592653589793238
Rounded value 3.14159265358979323846264338327950288419716939937510 to 19 decimal places: 3.1415926535897932385
Rounded value 3.14159265358979323846264338327950288419716939937510 to 20 decimal places: 3.14159265358979323846

Usage

The following illustrates the code being compiled and executed using the input 255.335.

java RoundDecimalExample [value]

[value] represents the number to round using the four methods; multiple numbers may be passed when separated by a space.
Rounding Double to Various Decimal Places in Java
Rounding Double to Various Decimal Places in Java

Source Code

import java.lang.Math;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class RoundDecimalExample {
  public static void main(String[] args) {
    double dValue = 0.0d;
    int i = 0;
    for(String strValue: args) {
      dValue = Double.parseDouble(strValue);
      // Method 1:  Double rounded to the nearest integer using Math.round().
      System.out.println("");
      System.out.println("Method 1:  Rounded using Math.round()");
      System.out.println("--------");
      System.out.println("Rounded value " + dValue + " using Math.round(): " + Math.round(dValue));
      // Method 2:  Double rounded to any decimal place by multiplying by a power of 10, converting to long, and dividing by a power of 10.
      System.out.println("");
      System.out.println("Method 2:  Rounded using Powers of 10");
      System.out.println("--------");
      for(i = 0; i <= 20; i++) {
        System.out.println("Rounded value " + dValue + " to " + i + " decimal places: " + RoundDecimalExample.roundDecimalUsingPowers(dValue, i));
      }
      // Method 3:  Double rounded to any decimal place using DecimalFormat.
      System.out.println("");
      System.out.println("Method 3:  Rounded using DecimalFormat");
      System.out.println("--------");
      for(i = 0; i <= 20; i++) {
        System.out.println("Rounded value " + dValue + " to " + i + " decimal places: " + RoundDecimalExample.roundDecimalUsingDecimalFormat(dValue, i));
      }
      // Method 4:  Double rounded to any decimal place using BigDecimal.
      System.out.println("");
      System.out.println("Method 4:  Rounded using BigDecimal");
      System.out.println("--------");
      for(i = 0; i <= 20; i++) {
        System.out.println("Rounded value " + strValue + " to " + i + " decimal places: " + RoundDecimalExample.roundDecimalUsingBigDecimal(strValue, i));
      }
    }
  }
  public static double roundDecimalUsingPowers(double dValue, int decimalPlace) {
    return ((long) ((dValue * Math.pow(10.0, decimalPlace)) + ((dValue < 0.0) ? -0.5 : 0.5))) / Math.pow(10.0, decimalPlace);
  }
  public static String roundDecimalUsingDecimalFormat(double dValue, int decimalPlace) {
    DecimalFormat decimalFormat = null;
    StringBuffer stringBuffer = null;
    stringBuffer = new StringBuffer("#");
    if(decimalPlace > 0) {
      stringBuffer.append(".");
    }
    for(int i = 1; i <= decimalPlace; i++) {
      stringBuffer.append("#");
    }
    decimalFormat = new DecimalFormat(stringBuffer.toString());
    decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
    return decimalFormat.format(dValue);
  }
  public static String roundDecimalUsingBigDecimal(String strValue, int decimalPlace) {
    return new BigDecimal(strValue).setScale(decimalPlace, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString();
  }
}

Generate All Permutations of a String in Java

Given any string, how many different permutations of that character set can be made? To answer that question, this Java program uses a recursive function to generate all permutations of a string passed as a command line argument. Most permutation generators that I have come across will only output permutations of the same length as the input set, e.g. enter 3 characters and the output will show permutations containing all 3 characters. This program, however, will generate all permutations of the input set from length 1 through n, where n is the total number of characters in the string. For example, if “abc” is entered, the program generates all permutations of length 1 to 3 characters: a, b, c, ab, ac, ba, bc, ca, cb, abc, acb, bac, bca, cab, cba.

Example Permutations of Varying Length for String "abc"
Example Permutations of Varying Length for String “abc”

Why is this useful? It probably isn’t. I wrote the code to see how the number of permutations grew with each additional character. I feel like this is also a standard introduction to Computer Science question so perhaps this will help someone do their homework assignment. From a practical standpoint, I suppose it could be used to help your Scrabble game. If you enter all of the letter tiles from your rack, this will generate all of the possible letter permutations. The code could be modified to validate those permutations against a Scrabble dictionary though it would be awfully suspicious if someone was typing into a computer/device throughout a game.

Usage

The following illustrates the code being compiled and executed using the sample set “abc”.

java Permutation [string]

[string] represents the set of characters to generate all permutations; multiple strings may be passed when separated by a space.
Java Program to Generate All Permutations of a String
Java Program to Generate All Permutations of a String

Source Code

import java.util.ArrayList;
public class Permutation {
  private ArrayList<String> permutations = null;
  private String characters = null;
  public static void main(String[] args) {
    Permutation p = null;
    ArrayList arrlist = null;
    for(String s: args) {
      p = new Permutation(s);
      arrlist = p.getPermutations();
      System.out.println("Generating all permutations of string: "+p.getCharacters());
      for(int i = 0; i < arrlist.size(); i++) {
        System.out.println(arrlist.get(i));
      }
      System.out.println("");
    }
  }
  public Permutation(String characters) {
    if((characters == null) || ("").equals(characters.trim())) {
      characters = "";
    }
    this.characters = characters;
  }
  public String getCharacters() {
    return characters;
  }
  public ArrayList getPermutations() {
    this.permutations = new ArrayList<String>();
    for(int j = 1; j <= getCharacters().length(); j++) {
      permute("",getCharacters(),j);
    }
    return this.permutations;
  }
  protected void permute(String prefix,String s,int length) {
    String word = null;
    for(int i = 0; i < s.length(); i++) {
      word = prefix+s.charAt(i);
      if(word.length() == length) {
        permutations.add(word);
      } else {
        if(i == 0) {
          permute(word,s.substring(i+1,s.length()),length);
        } else if(i == (s.length()-1)) {
          permute(word,s.substring(0,s.length()-1),length);
        } else {
          permute(word,(s.substring(0,i)+s.substring(i+1,s.length())),length);
        }
      }
    }
    return;
  }
}