Example usage for java.lang StringBuffer reverse

List of usage examples for java.lang StringBuffer reverse

Introduction

In this page you can find the example usage for java.lang StringBuffer reverse.

Prototype

@Override
public synchronized StringBuffer reverse() 

Source Link

Usage

From source file:atlas.kingj.roi.FrmMain.java

String formatDecimal(double d) {
    boolean neg = false;
    if (d < 0)
        neg = true;//from w ww  .  ja  v a 2  s.com
    double rounded = roundTwoDecimals(d);
    String res = Double.toString(rounded);
    if (neg)
        res = res.substring(1);
    String result = "";

    if (res.contains("E"))
        return res;

    // insert commas

    int unitcol = 0;
    if (res.charAt(res.length() - 1) == '.') {
        unitcol = 1;
    } else if (res.charAt(res.length() - 2) == '.') {
        unitcol = 2;
    } else if (res.charAt(res.length() - 3) == '.') {
        unitcol = 3;
    } else {
        unitcol = 0;
    }
    int count = 0;
    for (int i = res.length() - 1; i > res.length() - 1 - unitcol; i--)
        result = result + res.charAt(i);
    for (int i = res.length() - 1 - unitcol; i >= 0; i--) {
        if (count != 0 && (count % 3 == 0))
            result = result + ",";
        result = result + res.charAt(i);
        count++;
    }

    StringBuffer buffer = new StringBuffer(result);

    //Reverse the contents of the StringBuffer  
    buffer = buffer.reverse();

    //Convert the StringBuffer back to a String  
    result = buffer.toString();

    if (neg)
        result = "-" + result;

    return result;
}