converting amount into millions nad billions java

Solutions on MaxInterview for converting amount into millions nad billions java by the best coders in the world

showing results for - "converting amount into millions nad billions java"
Bryn
29 Jun 2016
1public String truncateNumber(float floatNumber) {
2    long million = 1000000L;
3    long billion = 1000000000L;
4    long trillion = 1000000000000L;
5    long number = Math.round(floatNumber);
6    if ((number >= million) && (number < billion)) {
7        float fraction = calculateFraction(number, million);
8        return Float.toString(fraction) + "M";
9    } else if ((number >= billion) && (number < trillion)) {
10        float fraction = calculateFraction(number, billion);
11        return Float.toString(fraction) + "B";
12    }
13    return Long.toString(number);
14}
15
16public float calculateFraction(long number, long divisor) {
17    long truncate = (number * 10L + (divisor / 2L)) / divisor;
18    float fraction = (float) truncate * 0.10F;
19    return fraction;
20}
21
similar questions