Abbreviating numbers in Android, locale sensitive - java

In my Android project, I am hoping to find a way to abbreviate numbers that is sensitive to various locales. If the number is less than 1000, it is to remain as is; otherwise, I would like the number divided by the largest possible power of 1000 and rounded to two decimal places. So far, I have the below code, which correctly produces the desired results as stated under Output.
public void formatNumbers() {
//Output:
//842 => 842
//24,567 => 24.57k
//356,915 => 356.92k
//7,841,234 => 7.84M
//1,982,452,873 => 1.98B
int[] i = new int[] {842, 24567, 356915, 7841234, 1982452873};
String[] abbr = new String[] {"", "k", "M", "B"};
DecimalFormat df = new DecimalFormat("0.00");
df.setRoundingMode(RoundingMode.HALF_UP);
for (long i1 : i) {
int thousands = thousands(i1);
String result;
if(thousands == 0) {
result = String.valueOf(i1);
} else {
double d = (double) i1 / Math.pow(1000.0, thousands);
result = df.format(d)+abbr[thousands];
}
System.out.println(i1 + " => " + result);
}
}
public int thousands(double num) {
//returns the number of times the number can be divided by 1000
int n=0;
double comp=1000.0;
while(num>comp) {
n++;
comp*=1000.0;
}
return n;
}
My concern is that I want a way to ensure that the output is sensitive to locale. If I understand correctly, DecimalFormat should take care of conventions such as commas instead of decimals where applicable; this leaves my concern with the suffix. While my output would be generally be understood in the United States for casual purposes (despite the fact that "M" is used to mean "thousands" in some industries, such as finance) and, from my understanding, many areas in Europe which have languages based on Latin, there are many locales that this would not be understood well. Perhaps there is a built-in function that handles this that I have not been able to find. Thank you in advance for your attention to and input on this task.

Related

Converting a double to a string fraction(shoe sizes)

So for my project I have to write a method to convert double values to a string. I understand how to do this multiple ways multiple options of formatting. What I'm confused about is how would I turn half sizes(shoes) into a fraction version while still converting it to a string. So
10.5
returns
10 - 1/2
Any tips or helpful pointers are appreciated. Sorry if this is a dumb question I am still learning. :)
What you want can be done with the modulus operator (%) which in Java can be used with floating point values. If you do shoeSize % 1 you will get 0.5 with half sizes and 0.0 with others, so you only have to check that value to add the "1/2" to the string representation or not. Here is a simple example
public class ShoeSize {
double size;
public ShoeSize(double size) { this.size = size; }
public String toString() {
return "" + (int)size + (size % 1 == 0.5? " 1/2" : "");
}
public static void main(String args[]) {
ShoeSize ss1 = new ShoeSize(10.5);
ShoeSize ss2 = new ShoeSize(11);
ShoeSize ss3 = new ShoeSize(11.5);
System.out.println(ss1);
System.out.println(ss2);
System.out.println(ss3);
}
}
The result of the previous code is:
10 1/2
11
11 1/2
However, you really shouldn't go with that approach. That is because depending on the way you manage the shoe size values it can lead to unpredictable results, just because arithmetic in floating point values is not precise as it is with integer values. Some simple operations like the following can introduce enough error so the result is not what you expect:
...
public static void main(String args[]) {
ShoeSize ss = new ShoeSize(10.0);
ss.size += 0.1 + 0.2 + 0.3; // Sum it half
System.out.println(ss);
}
This code now prints 10 instead of 10 1/2.
What should you do instead? Well, there are several ways. You could for example store the shoe size inside ints representing, with each unit representing a half. This internal representation will be much error-prone if you have operations like addSize or subtracts. The only problem will be reading the size of the user; the best way is probably having a list of predefined sizes for the user to choose. Here is an example:
public class ShoeSize {
int halves;
public ShoeSize(double size) { this.halves = (int)(size * 2); }
public String toString() {
return "" + (halves / 2) + (halves % 2 == 1? " 1/2": "");
}
public static void main(String args[]) {
ShoeSize ss = new ShoeSize(10.5);
System.out.println(ss);
}
}
Still better, since the shoe sizes use to be very restricted between certain values, you could represent their values in a single enum. Every enum can be constructed from the human-readable string of the size (ex. "10 1/2") and there would never be problems with invalid shoe sizes. The only problem with this approach is the need to define a custom method to obtain the next and previous shoe sizes, but here is a question that can help you with that:
[What's the best way to implement `next` and `previous` on an enum type?
Try this
public String getShoeSize(double size) {
int s = (int)size;
String shoeSize = Integer.toString(s);
//check if it is a half value
if(size > s) {
shoeSize += " - 1/2";
}
return shoeSize;
}
At first convert value of double to string and split it:
double d=10.5;
String s = String.valueOf(d);
String[] newstr =s.split(".");
then if fraction is limited use switch case for .5 , .25 , .75
but it is not limited ,simplifying fractions is easy if you can folow the steps:
pay attention to this point that maybe your number is not point sign for example 10 that in switch case must be considered
1.find gcd of both num and den, so you have gcd=GCDFind(gcd, num);
2.now, if gcd==1, then the fraction cannot be simplified (it is already in simplified form).
3.if gcd > 1, then newNum = num/gcd; and newDen = den/gcd;
example for limited :
double s = 10.25;
String aString = Double.toString(s);
String[] fraction = aString.split("\\.");
int denominator = (int)Math.pow(10, fraction[1].length());
int numerator = Integer.parseInt(fraction[0] + "" + fraction[1]);
int t=numerator%denominator;
switch(t){
case 0: System.out.println(numerator/denominator);break; //example : 10
case 5: System.out.println(numerator/denominator + " - " +"1/2");break; //example : 10.5
case 25: System.out.println(numerator/denominator + " - " +"1/4");break; //example : 10.25
case 75: System.out.println(numerator/denominator + " - " +"3/4");break; //example : 10.75
default:System.out.println("Not in 1/2, 1/4 or 3/4");
}

How to get rid of leading zero before the decimal point in a double less than one?

I am doing a rather large project (for me, is an AP JAVA course in high school). Anyways, for a portion of the project I need to be able to round a decimal and output it without the zero at the beginning before the decimal.
Ex: .4999 rounds ---> 0.5
I need: .4999 round ---> .5
Thanks in advance
As Ingo mentioned, you'll need to get a String representation of the number in order to modify the output as desired. One solution would be to use java.text.NumberFormat. setMinimumIntegerDigits(0) seems to fit the bill. I'm sure there are plenty more options as well.
Try this:
public static String format(double value, int decimalPlaces) {
if (value >= 1 || value < 0) {
throw new IllegalArgumentException("Value must be between 0 and 1");
}
final String tmp = String.format("%." + decimalPlaces + "f", value);
return tmp.substring(tmp.indexOf('.'));
}
Examples:
System.out.println(format(0.4999d, 1)); // .5
System.out.println(format(0.0299d, 2)); // .03
System.out.println(format(0.34943d, 3)); // .349
Working Fiddle
The DecimalFormat class can be used.
DecimalFormat df = new DecimalFormat("####0.0");
System.out.println("Value: " + df.format(value));
May seem weird to you, but you can use regex to strip off the leading zero like this:
(assuming that you already know how to round the decimal place to one)
double value = 0.5;
String val = Double.toString( value ).replaceAll( "^0(\\..*)$", "$1" );
System.out.println( val );
Console:
.5

java equivalent to printf("%*.*f")

In C, the printf() statement allows the precision lengths to be supplied in the parameter list.
printf("%*.*f", 7, 3, floatValue);
where the asterisks are replaced with first and second values, respectively.
I am looking for an equivalent in Android/Java; String.format() throws an exception.
EDIT: Thanks, #Tenner; it indeed works.
I use
int places = 7;
int decimals = 3;
String.format("%" + places + "." + decimals + "f", floatValue);
A little ugly (and string concatenation makes it not perform well), but it works.
System.out.print(String.format("%.1f",floatValue));
This prints the floatValue with 1 decimal of precision
You could format the format :
String f = String.format("%%%d.%df", 7, 3);
System.out.println(f);
System.out.format(f, 111.1111);
This will output :
%7.3f
111,111
You could also use a little helper like this :
public static String deepFormatter(String format, Object[]... args) {
String result = format;
for (int i = 0; i != args.length; ++i) {
result = String.format(result, args[i]);
}
return result;
}
The following call would then be equivalent as the code above and return 111,111.
deepFormatter("%%%d.%df", new Object[] {7, 3}, new Object[] {111.1111});
It's not as pretty as printf, and the input format can become cluttered, but you can do much more with it.
Its like this...
%AFWPdatatype
A - Number of Arguments
F - Flags
W - Width
P - Precision
String.format("%.1f",float_Val);

Any neat way to limit significant figures with BigDecimal

I want to round a Java BigDecimal to a certain number of significant digits (NOT decimal places), e.g. to 4 digits:
12.3456 => 12.35
123.456 => 123.5
123456 => 123500
etc. The basic problem is how to find the order of magnitude of the BigDecimal, so I can then decide how many place to use after the decimal point.
All I can think of is some horrible loop, dividing by 10 until the result is <1, I am hoping there is a better way.
BTW, the number might be very big (or very small) so I can't convert it to double to use Log on it.
Why not just use round(MathContext)?
BigDecimal value = BigDecimal.valueOf(123456);
BigDecimal wantedValue = value.round(new MathContext(4, RoundingMode.HALF_UP));
The easierst solution is:
int newScale = 4-bd.precision()+bd.scale();
BigDecimal bd2 = bd1.setScale(newScale, RoundingMode.HALF_UP);
No String conversion is necessary, it is based purely on BigDecimal arithmetic and therefore as efficient as possible, you can choose the RoundingMode and it is small. If the output should be a String, simply append .toPlainString().
You can use the following lines:
int digitsRemain = 4;
BigDecimal bd = new BigDecimal("12.3456");
int power = bd.precision() - digitsRemain;
BigDecimal unit = bd.ulp().scaleByPowerOfTen(power);
BigDecimal result = bd.divideToIntegralValue(unit).multiply(unit);
Note: this solution always rounds down to the last digit.
Someone will probably come up with a better solution, but the first thing that comes to mind is chuck it in to a StringBuilder, check whether it contains a '.' and return an appropriate length substring. E.g.:
int n = 5;
StringBuilder sb = new StringBuilder();
sb.append("" + number);
if (sb.indexOf(".") > 0)
{
n++;
}
BigDecimal result = new BigDecimal(sb.substring(0, n));
To me this seems as simple as:
Given N = 5, D = 123.456789
get the string representation of the number, "123.456789"
retrieve the first N-1 digits of the number, "123.4"
evaluate D[N] and D[N+1], in this case "5" and "6"
6 meets the criteria for rounding up (6 > 4), therefore carry 1 and make D[N] = 5+1 = 6
D post rounding is now 123.46
Order can be calculated using Math.floor(Math.log(D)).
hope this helps.
Since BigDecimal is basically a string, perhaps this:
import java.math.BigDecimal;
public class Silly {
public static void main( String[] args ) {
BigDecimal value = new BigDecimal("1.23238756843723E+5");
String valueString = value.toPlainString();
int decimalIndex = valueString.indexOf( '.' );
System.out.println( value + " has " +
(decimalIndex < 0 ? valueString.length() : decimalIndex) +
" digits to the left of the decimal" );
}
}
Which produces this:
123238.756843723 has 6 digits to the left of the decimal
A.H.'s answer is technically correct, but here is a more general (and easier to understand) solution:
import static org.bitbucket.cowwoc.requirements.core.Requirements.assertThat;
/**
* #param value a BigDecimal
* #param desiredPrecision the desired precision of {#code value}
* #param roundingMode the rounding mode to use
* #return a BigDecimal with the desired precision
* #throws NullPointerException if any of the arguments are null
*/
public BigDecimal setPrecision(BigDecimal value, int desiredPrecision, RoundingMode roundingMode)
{
assertThat("value", value).isNotNull();
assertThat("roundingMode", roundingMode).isNotNull();
int decreaseScaleBy = value.precision() - desiredPrecision;
return value.setScale(value.scale() - decreaseScaleBy, roundingMode);
}

Convert fraction to decimal number

i'm doing some exercises in my Java book. I'm very new to programming. Therefore, notice (in the code) that i'm still on Chapter one. Now I already did everything, I just want a confirmation if this is legitimate so I can feel free to move on next.
If not, I would sincerely appreciate to not do my code for me; I want advice.
Here's the question written in the book,
"Write an application that prompts/reads the numerator and denominator of a fraction as integers, then prints the decimal equivalent of the fraction."
I'll illustrate this sentence with my code:
I did a revision here. Is this one OK?..
import java.util.*;
public class ExerciseEleven {
public static void main (String[] args) {
Scanner sc = new Scanner (System.in);
double fraction;
int fractionValue;
int decimal;
double value;
System.out.println("Enter Numerator: ");
int numerator = sc.nextInt();
System.out.println("Enter Denominator: ");
int denominator = sc.nextInt();
fraction = (double) numerator / denominator;
fractionValue = (int) (fraction * 10);
decimal = fractionValue % 10;
value = decimal * 0.1;
System.out.println(value);
}
}
It compiles and works fine.
Thank you.
It doesn't do what task says it should. You read doubles instead of integers, and the decimal equivalent is not what you print out. Decimal equivalent for 1/2 is 0.5. And you print 5.
Also, you can pay attention to your code style: variable names are usually written in lowerCamelCase, like that : simpleVariable.
Update
now it prints what you need. However you do it not in the very right way and your indentation can still be improved.
It's fine (I didn't read the assignment very well, did I? Kudos to Vladimir.) ...but some comments:
Usually you want to indent methods within the class.
Standard practice is to use initial caps (Numerator) only for types (e.g., classes, interfaces, enums). Variable, field, and method names should start with a lower-case letter. Now, you're free to ignore standard practice, but if you do people will have a lot of trouble reading your code. :-)
For rounding, you probably want to look at Math.round rather than truncating with a cast. But the assignment didn't say anything about rounding.
You might want to handle the case where denominator is zero.
So keeping those in mind:
import java.util.*;
public class ExcerciseEleven {
public static void main (String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("Enter Numerator: ");
int numerator = sc.nextInt();
System.out.println("Enter Denominator: ");
int denominator = sc.nextInt();
if (denominator == 0) {
System.out.println("Can't divide by zero");
}
else {
double fraction = (double)numerator / denominator;
System.out.println(fraction);
}
}
}
Hey I am doing some thinking about this and I have noticed something interesting after looking at this source and here is the Algorithm that I plan on implementing
First I will convert the number from the Metric using the
Javax.Measure family of functions and I will get a number like
0.3750
Then I will divide the number by ONE_SIXTEENTH which = 0.0625
ONE_SIXTEENTH = 0.0625
The answer 0.3750 / ONE_SIXTEENTH = 6;
So now I know there are 6 sixteenths of the inch
Next I check to see if 6 is divisible by 4, 6/4 = 1.5 ie not a whole number so the fraction is still regarded as 6/16th of an inch for now
Next I check to see if 6 is divisible by 2, 6/2 = 3
This is a whole number so we will use it to reconstitute the fraction
So now that we have divided 6 by 2 and gotten 3 the 16 needs to be divided by 2 and we end up with 8 so 6/16th of an inch becomes 3/8th of an inch.
PS Has anyone noticed that this is similar to a fizz bang program?
____________________________________________
Here is the chart which helped me get my head around this
My workings
There are three important parts of division operation :
Sign of the result.
Integral part
Decimal part
Also, there are few corner cases where you need to deal with the fact that Integer.MIN_VALUE is greater than Integer.MAX_VALUE when compared in absolute form.
For example : -2147483648/-1 can't yield 2147483648 when divided in the form of integer types. The reason is simple. The type of the resulting type will be integer type, and the maximum positive value that a integer type variable can hold is +2147483647
To mitigate that scenario, we should at first convert both the numerator and denominator into their long positive form. That gives us the integral part of the answer.
The XOR of two numbers will have the sign bit as 1 only in case they have opposite signs. That solves the first part (sign of result) of the problem.
For decimal part, we can employ the general division rule i.e. multiply the remainder with 10 and try dividing again and repeat. Keep record of the remainder we have already come across to prevent the loop from going into unbounded iterations.
public String fractionToDecimal(int A, int B) {
StringBuilder sb = new StringBuilder((A^B) < 0 ? "-" : "");
long a = Math.abs((long)A);
long b = Math.abs((long)B);
sb.append(Long.toString(a/b));
long rem = a % b;
sb.append((rem != 0) ? "." : "");
Map<Long, Integer> remainderMap = new HashMap<>();
int pos = 0;
while (rem != 0){
sb.append(Long.toString((rem*10)/b));
remainderMap.put(rem, pos++);
rem = (rem*10) % b;
if (remainderMap.containsKey(rem)){
String currNum[] = sb.toString().split("\\.");
return currNum[0] + "." + currNum[1].substring(0, remainderMap.get(rem)) +
"(" + currNum[1].substring(remainderMap.get(rem)) + ")";
}
}
if (sb.toString().equals("-0")) return "0";
return sb.toString();
}
Sample output :
2/3 gives 0.(6)
-2147483648/-1 gives 2147483648

Categories

Resources