0

Sorry I was initially wanting to do it in php PHP integer part padding, but realised I will do it in Java in another part of code

So I need to format numbers with the integer part at least 2 chars

2.11 -> 02.11
22.11 -> 22.11
222.11 -> 222.11
2.1111121 -> 02.1111121

double x=2.11; 
System.out.println(String.format("%08.5f", x));

could do it, but it's annoying the right trailing zeros, I would like to have an arbitrary large floating part

String.format("%02d%s", (int) x, String.valueOf(x-(int) x).substring(1))

is totally ugly and unexact (gives 02.1099...)

new DecimalFormat("00.#############").format(x)

will truncate floating part

thx for any better solutions

Community
  • 1
  • 1
  • 2
    change it to `String.format("%8.5f", x)` (without the 0 before the 8). – Luiggi Mendoza Jul 08 '12 at 14:25
  • I need to pad integer part with 0, to have at least 2 characters for integer part –  Jul 08 '12 at 14:27
  • then change it to `String.format("%02.5f", x)`, if the number has more than 2 digits in the integer part, it will print the whole integer part. – Luiggi Mendoza Jul 08 '12 at 14:28
  • it gives 2.11000, I need 02.11 –  Jul 08 '12 at 14:30
  • Are you aware of how many digits do you need on the decimal part? Because you can use `String.format("%02f", x)` without problems. I guess you should put more examples about what you need/want. You can even use a [`NumberFormat`](http://www.exampledepot.com/egs/java.text/FormatNum.html) object to do the job for you (but it will require more lines of code) – Luiggi Mendoza Jul 08 '12 at 14:33
  • You also need to be aware of the semantics of a J – Marko Topolnik Jul 08 '12 at 14:37
  • Yes NumberFormat with 00.# seems close, but I don,t really want to truncate floating part 05.33231554255 for ex –  Jul 08 '12 at 15:34

2 Answers2

0

the best I could come with is

public static String pad(String s){
    String[] p = s.split("\\.");
    if (2 == p.length){
        return String.format("%02d.%s", Integer.parseInt(p[0]), p[1]);
    }
    return String.format("%02d", Integer.parseInt(p[0]));
}

pad(String.valueOf(1.11111118)) -> 01.11111118

-1

Here's an one-liner using DecimalFormat:

new DecimalFormat("00." + (x + "").replaceAll(".", "#")).format(x)

It formats your decimal as 00.#############..., where the length of "#############..." comes from the length of your decimal ("#"s in excess does nothing).

You can use String.valueOf(x) in place of (x + "") if you wish.

  • I'd prefer not truncate, to not lose accuracy –  Jul 08 '12 at 15:37
  • yes nice, like new DecimalFormat("00." + StringUtils.repeat("#", (x+"").length())).format(x) –  Jul 08 '12 at 17:03
  • "#'s in excess does nothing": try this: float f = (float) 22.2222; System.out.println(new DecimalFormat("00." + (""+f).replaceAll(".", "#")).format(f)); –  Jul 14 '12 at 21:42
  • # isn't the cause. Y'know, doubles and floats are limited in all programing languages. And even, that limitation varies according to where and how you use the value. See [this related question](http://stackoverflow.com/a/1420758/1493676), answered by Stack Overflow's #1 Jon Skeet, as well as his [famous talk](https://msmvps.com/blogs/jon_skeet/archive/2009/11/02/omg-ponies-aka-humanity-epic-fail.aspx) about many cases like that. –  Jul 17 '12 at 21:34