0

I need to put the decimal separator point in a Long, I have tried in several ways, but I need it to be dynamic since the decimal separator can change, I have tried with DecimalFormat format = new DecimalFormat("###.##"); but this is not dynamic and it doesn't work the way I wanted it to

Example 1

long amount = 123456;

int decimal = 2;

The result should be Double newAmount = 1234.56

Example 2

long amount = 123456;

int decimal = 4;

The result should be Double newAmount = 12.3456

  • "The result should be" — the result of doing _**what**_ should be 1234.56? `Long` and `long`, like `Integer` and `int` hold only integral values; they don't use decimal places, so what are you doing to the "Long amount" and the "decimal = 2" to try to get your result? – Stephen P Jan 08 '21 at 00:35
  • Long values are whole numbers. Where would the decimal point go? – Bohemian Jan 08 '21 at 00:39
  • you just have to divide / 10^decimal places for it. No formatting involved. And make that long a double or declare the dividend as double, or you lose the decimal part – aran Jan 08 '21 at 00:47

3 Answers3

1

If I understand correctly, this is what you are trying to achieve:

Long amount = 123456;
int decimal = 2;
double newAmount = amount.doubleValue();
newAmount = newAmount / Math.pow(10, decimal);

Use the pow method of java.lang.math to calculate the power of a number.

Be careful to declare your variable as an object of type Long and not a primitive type if you want to use one of its functions.

As suggested, it is even simpler to just use a double variable instead of a long from the start:

double amount = 123456;
int decimal = 2;
amount = amount / Math.pow(10, decimal);
Thanasis M
  • 1,144
  • 7
  • 22
  • What is `l` in your code? It is not declared or set anywhere. – Stephen P Jan 08 '21 at 00:47
  • @StephenP You are right, it was a typo, I wrote `l` by mistake because I was thinking that it is a long variable. Thank you!! – Thanasis M Jan 08 '21 at 00:50
  • 1
    Ok, you replaced `l` — but a **primitive** `long` like amount doesn't have methods; it is a _primitive_. You can't do `longvalue.doubleValue()`, you would need to use a `Long` object, not a primitive. – Stephen P Jan 08 '21 at 00:51
  • *just declare one of amount/decimal as double* No need for doubleValue() or Long objects – aran Jan 08 '21 at 00:54
  • @StephenP Correct, I edited my answer. So, yeah be careful to use an object of type Long and not a primitive type in order to use one of its functions. Certainly, there are many other similar ways to dynamically add a decimal separator point to a number, but I think this is the most simple and easier to comprehend. – Thanasis M Jan 08 '21 at 00:56
  • 1
    You actually _can_ do it without objects, as aran says, even if you're not allowed to change the declared types of `amount` or `decimal` because the return type of `Math.pow` is `double`. If you have `long amount` and `int decimal` like in the OP question, you can do: `double newAmount = amount / java.lang.Math.pow(10, decimal);` – Stephen P Jan 08 '21 at 01:02
  • 1
    @aron I added your suggestion. I wanted to use the OP's example's initial variables as they are, to provide a better explanation and in case he had to use necessarilly these type of variables. But, I added your improvement, if that's not the case. – Thanasis M Jan 08 '21 at 01:08
0

You can get the required number by dividing the given number by 10 ^ decimalPlaces e.g.

public class Main {
    public static void main(String[] args) {
        // Test
        System.out.println(getNum(123456, 2));
        System.out.println(getNum(123456, 4));
    }

    static double getNum(long val, int decimalPlaces) {
        return val / Math.pow(10, decimalPlaces);
    }
}

Output:

1234.56
12.3456
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

All the other answers suggest converting to double and then scaling by powers of 10 before displaying. This will result in some unexpected results because of a loss of precision in the scaling operation. For the complete, gory details on why, please read What Every Computer Scientist Should Know About Floating-Point Arithmetic and Is Floating Point Broken?

As to your problem, you should be doing the work using BigDecimal. Converting from long (or Long) to BigDecimal does not lose precision, and will always produce the expected results.

BigDecimal even has a method to do the scaling for you:

long amount = 123456;
int decimal = 2;
BigDecimal n = BigDecimal.valueOf(amount).scaleByPowerOfTen(-decimal);

Output:

1234.56
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190