0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

自分用メモ#5

Posted at

数値加工の奴

public class DecimalFormatter {

    public static String formatDecimal(String number, int integerPartLength) {
        // 入力のバリデーション
        if (number == null || number.isEmpty()) {
            throw new IllegalArgumentException("Number string cannot be null or empty");
        }
        if (integerPartLength < 1 || integerPartLength > 8) {
            throw new IllegalArgumentException("Integer part length must be between 1 and 8");
        }

        // 入力が8桁未満の場合、ゼロ埋め
        while (number.length() < 8) {
            number = "0" + number;
        }

        // 小数点を挿入
        String integerPart = number.substring(0, integerPartLength).replaceFirst("^0+", ""); // 整数部の先頭のゼロを除去
        if (integerPart.isEmpty()) {
            integerPart = "0";
        }
        String fractionalPart = number.substring(integerPartLength); // 小数部

        // 整数部と小数部を結合して返す
        return integerPart + "." + fractionalPart;
    }

    public static void main(String[] args) {
        // サンプル入力
        String number = "1";
        int integerPartLength = 5;

        // 結果を表示
        try {
            String result = formatDecimal(number, integerPartLength);
            System.out.println("Formatted number: " + result);
        } catch (IllegalArgumentException e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?