On Thu, 18 Jul 2024 11:32:47 GMT, Shaojin Wen <d...@openjdk.org> wrote:

> class LocalTime {
>     public String toString() {
>         // ...
>                 if (nanoValue % 1000_000 == 0) {
>                     buf.append(Integer.toString((nanoValue / 1000_000) + 
> 1000).substring(1));
>                 } else if (nanoValue % 1000 == 0) {
>                     buf.append(Integer.toString((nanoValue / 1000) + 
> 1000_000).substring(1));
>                 } else {
>                     buf.append(Integer.toString((nanoValue) + 
> 1000_000_000).substring(1));
>                 }
>        // ...
>     }
> }
> 
> Currently, LocalTime.toString handles nanos by adding a value and then 
> subString(1) to fill it with zeros. Using StringBuilder.repeat is more 
> concise and has better performance.

Looking at #20229, can we create another utility method in jdk.internal like in 
`DecimalDigits` to append to a StringBuilder a decimal output with a given 
field width? Like

public static void printFixedWidth(StringBuilder sb, int width, int value) {
    var size = JLA.stringSize(value);
    sb.repeat('0', width - size);
    sb.append(value);
}

And call this in `LocalTime` `LocalDate` `Formatter` etc.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/20232#issuecomment-2236463542

Reply via email to