0

i want to change 2.5 to 2½. Not sure how to change it.

Let say I have “AMZN 2½ 22” in a long text that I have to highlight in java swing textpane but I have values in three variable as below

A = AMZN
B = 2.5
C = 22

Based on this value I will not be able to match so changing B to Rational number (String) and then matching and it’s working fine but I don’t want to do this… it’s just a temp fix.

Can someone help me ?

B = B.toString().replace(".25", "¼")
                        .replace(".5", "½") 
                        .replace(".75", "¾")
                        .replace(".375", "⅜")
                        .replace(".625", "⅜")
                        .replace(".125", "⅛")
                        .replace(".875", "⅞")
                        .replace(".0", "")
                        .replace(".000", "")
                        .replace(".00", "");

Thanks

Rajiv525
  • 141
  • 2
  • 10
  • More information needed. How do you store 2.5? Where do you want to show 2½? What part of the changing process is problematic? What have you tried? – Deltharis Sep 15 '14 at 11:33
  • Let say I have “AMZN 2½ 22” in a long text that I have to highlight in java swing textpane but I have values in three variable as below A = AMZN B = 2.5 C = 22 Based on this value I will not be able to match so changing B to Rational number (String) and then matching and it’s working fine but I don’t want to do this… it’s just a temp fix. B = B.toString().replace(".25", "¼") .replace(".5", "½") – Rajiv525 Sep 15 '14 at 11:41

3 Answers3

0

Here's how to print the symbol 'half':

String half = "\u20BD";

System.out.println("2" + half);

determining if a number is an integer plus half is left as an exercise to the reader.

Boris Pavlović
  • 63,078
  • 28
  • 122
  • 148
0

You can swap .0 replaces with a regex (replaceAll("\\.[^1-9]+","" comes to mind), you can move the replaces to a map or utility class, have them happen only when Float.parseFloat(var) doesn't throw NumberFormatException...

But there is no truly better way to do such arbitrary replaces. Those unicode strings have no intrinsic connection with the numbers they represent that you could use.

Deltharis
  • 2,320
  • 1
  • 18
  • 29
0

There are two alternatives for real numbers: double (an approximation of a real number) or BigDecimal (maintaining a precission).

String a = "AMZN";
BigDecimal b = new BigDecimal("2.5");
double b2 = 2.5;
int c = 22;

// %s = string, %f = floating point, %d = digits, %n = newline.
System.out.printf("%s %f %d%n", a, b2, c);

For double you have not really any control but you might format the output (printf) using "%.3f" for a precission of 3 decimals.

In your case you want to represent the numbers using fractions from the Unicode.

Let's do that with the less suited double:

System.out.printf("%s %s %d%n", a, asFraction(b2), c);

static String[] fractionTexts =  { "", "¼", "½", ... }; // Maybe char
static double[] fractionValues = { 0.0, 0.25, .5, ... };

static String asFraction(double x) {
    if (x < 0) {
        return "\u2212" + asFraction(-x); // U+2212 is Unicode minus.
    }
    long integralPart = (long)x;
    double decimalsPart = x - integralPart;

    for (int i = 0; i < fractionValues.length; ++i) {
        if (almostEqual(decimalsPart, fractionValues[i]) {
            decimalPoint = "";
            return MessageFormat("{0}", integralPart) + fractionTexts[i];
        }
    }
    return MessageFormat("{0}", x);
}

private boolean almostEqual(double x, double y) {
    final double EPS = 0.0001;
    return x >= y - EPS && x <= y + EPS;
}

The code uses MessageFormat for thousand separators / decimal separator. For 0.0, 0.00, 0.00ß0 (the same number) it leaves away the numbers explicitly. The java source code must be in the same encoding as the java compiler and be able to hold ¼ and others (like UTF-8).

For the error bearing double I have introduced almostEqual.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138