1

My question is basically the following:

When I use a value with BigDecimal, how do I append zeros in front of a random number? Say I want to have a number <10 following an entirely random pattern. Now i want to add zeros in front of the number, so the actual amount adds up to 10 numbers.

Here's an example: BigDecimal num = new BigDecimal(2353);

Now I want to have that ouput: 0000002353

Is there a function that appends numbers to a BigDecimal type? I couldn't find any.

I tried using a while loop that checks whether the number is less than ten. But I don't understand the Big Decimal well enough to actually compare integral values to the BigDecimal types. Thanks for any help in advance!

Alexander Ivanchenko
  • 25,667
  • 5
  • 22
  • 46
Noname
  • 69
  • 7
  • 1
    `BigDecimal` is a numeric type. You can't prepend zeros to a number, because as far as the memory is concerned, there already are zeros there. It would be weird under normal circumstances to display them, so it doesn't display them. Convert it to a string then prepend them to the string. – Jesse Nov 18 '22 at 21:19
  • 1
    Firstly, numeric types have no inherent formatting options like padding. Do you want to force output to a fixed length of 10 digits? What is to happen if the BigDecimal has more than that many digits? Are digits after the decimal point counted? Does the decimal point itself count for length? – Bohemian Nov 18 '22 at 21:19
  • Why would you want this? If you are performing math operations with the numbers, why would you need leading zeroes. If you are not performing math on the numbers then you don't need `BigDecimal` and you can just use `String` to store the values., including leading zeroes. If you need to perform math _then_ display with leading zeroes, then you simply add the padding when displaying. – Nexevis Nov 18 '22 at 21:26
  • @Bohemian Exactly. The output has to be exactly 10 digits and there should be zeros in front of the number. If there is no numeric way of doing that using the BigDecimal type, then I need to use a string probably. Decimal points are not permitted, so I just want to add zeros to an integral type. If the number itself has ten digits already. Nothing should happen.. – Noname Nov 18 '22 at 21:26
  • If all you want is to print the number with zeros and you don't care about the number itself, you can format the output like that `System.out.printf("%s%s", "0".repeat(Math.max(0, 10 - String.valueOf(num).length())), num);` – Melron Nov 18 '22 at 21:28
  • Also, if no decimal numbers are permitted, then you don't need a `BigDecimal`, not even a `BigInteger`. `Long` should do. – Jetto Martínez Nov 18 '22 at 21:30
  • If decimal points are not permitted, you should use `BigInteger`, not `BigDecimal` – Bohemian Nov 18 '22 at 22:30

4 Answers4

2

If you use a BigInteger instead (or any integer type, such as int or long) you can format the value with

String.format("%010d", BigInteger.valueOf(2353))

The leading 0 in the format strings means pad with 0, the following 10 is the desired length...

Per Huss
  • 4,755
  • 12
  • 29
1

BigDecimal is meant to be used for storing large floating point numbers. Since in a floating-point number there isn't any difference between 0000002353 and 2353, there is no reasonable way to append leading 0's to a BigDecimal just as there is no reasonable way to append leading 0's to a normal float. According to the behavior you're looking for, I would suggest using a String to store your number, and then convert to and from BigDecimal when you want to perform any operations.

To compare an integral type to a BigDecimal, first convert the variable to a BigDecimal and then call BigDecimal's compareTo method. More info is in this question.

human bean
  • 847
  • 3
  • 15
  • Ok interesting. So when I would use a string and then convert it afterwards to a BigDecimal.. would the zeros get truncated afterwards either way? – Noname Nov 18 '22 at 21:21
  • 2
    @Eldinur No. "outputting" means *rendering* as a string, which is not strictly reversible. To output with leading zero padding, convert to a string then add the zeros. When parsing a string to a numeric type leading zeroes are ignored. – Bohemian Nov 18 '22 at 21:24
  • @Bohemian Ok. Then there is no such thing as a BigDecimal, which carries zeros in front of its actual number, right? – Noname Nov 18 '22 at 21:30
  • @Eldinur Every single number does that. If you have a `short` that has a `2` in it, the binary representation (converted to human-readable form) is `00002`. A short always has 5 digits, no more, no less. However as I said in my previous comment, under normal circumstances it would be weird, and frankly quite annoying to display it this way, so part of converting it to human-readable format (a string) is removing these redundant zeros. If you want zeros there, put them back after it's converted to a string. – Jesse Nov 18 '22 at 21:37
1

Since you're interested in formatting the number, you might want to look at DecimalFormat class, which allows to format floating point and integer numbers according to the specified pattern.

BigDecimal num = new BigDecimal(2353);
        
DecimalFormat f1 = new DecimalFormat("0000000000");
DecimalFormat f2 = new DecimalFormat("0,000,000,000");
        
System.out.println(f1.format(num));
System.out.println(f2.format(num));

Output:

0000002353
0,000,002,353
Alexander Ivanchenko
  • 25,667
  • 5
  • 22
  • 46
0

If the maximum number of digits is 10 and only whole numbers are allowed you don't need anything more than to use long with standard formatting:

long myNumber = 123456;
System.out.printf("%010d%n", myNumber);
Bohemian
  • 412,405
  • 93
  • 575
  • 722