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");
}
Related
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.
My goal is to get a String of ones and zeroes from input, then use the bin2Dec method to parse that binary string into a decimal number. How can I convert this correctly?
Here is what I have so far:
public class Tester {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a binary number string: ");
String s = input.nextLine();
System.out.println("The decimal value is " + bin2Dec(s));
}
public static int bin2Dec(String binaryString){
}
}
The algorithm to do this is actually pretty simple.
If you look at a binary string, say, "10110" (22), what you'll notice is that equals 16 + 4 + 2. Look at this a bit harder, and you'll see it corresponds to 1 * 2^4 + 0 * 2^3 + 1 * 2^2 + 1 * 2^1 + 0 * 2^0. See the pattern? For each digit with value v and position n, it's "contribution" to the total sum is v * 2^n.
Now, this problem is actually easier to do iteratively (With a for-loop), but I assume this is a homework question and therefore you have to do it recursively.
You'll need to make another method, public static int bin2dec(String bin, int position) (this is called overloading the method, two methods can have the same name as long as their signatures are different). In this new method, you can follow the algorithm outlined above:
If position equals bin.length(), you've gone past the end of the string. Just return 0.
Otherwise, set v equal to the integer value of the character at position (You can either use the integer parse method, or just an if/ternary because there are only two options).
Multiply v by 2^position (hint: Math.pow).
Return v plus bin2dec(bin, position + 1) (this is the actual recursive part).
Now, in the original bin2dec, you can just put return bin2dec(bin, 0). That just allows you to call bin2dec without initializing the recursive counter, which is just general bookkeeping that callers don't want to deal with (not so important here, but very important in larger projects).
public static String showAsBinString(int n, String s){
if(n > 1){
s = showAsBinString(n/2, s);
}
s += n%2 == 0 ? "0" : "1";
return s;
}
System.out.println(showAsBinString(45, ""));
101101
it´s a simple task but i´m not able to solve it on my own..
i got
double digit1 = 12.1;
double digit2 = 12.99;
and need a method which gives me this:
anyMethod(digit1); //returns 10
anyMethod(digit2); //returns 99
what i have is
public static void getAfterComma(double digit) {
BigDecimal bd = new BigDecimal(( digit - Math.floor( digit )) * 100 );
bd = bd.setScale(4,RoundingMode.HALF_DOWN);
System.out.println(bd.toBigInteger()); // prints digit1=1 and digit2=99
}
anyway i prefer integer as the returntype..
anybody got a quick solution/tip?
kindly
Why not you simply use:
int anyMethod(double a){
//if the number has two digits after the decimal point.
return (int)((a + 0.001) * 100) % 100;
}
A simple way of getting the fractional part of a double is to use the modulo operator, %. However, you'll have to take into account the fact that floating point arithmetic isn't precise. For example,
System.out.println(12.1 % 1); // outputs 0.09999999999999964
System.out.println(12.99 % 1); // outputs 0.9900000000000002
If you want to get two decimal digits as an int, which is what I think you're asking, you can achieve this, glossing over the floating point issues, like so:
System.out.println(Math.round((12.1 % 1) * 100)); // outputs 10
System.out.println(Math.round((12.99 % 1) * 100)); // outputs 99
However, you should consider going further down the BigDecimal path you started down, which uses arbitrary precision arithmetic. You could do something like this:
System.out.println(new BigDecimal("12.1").remainder(BigDecimal.ONE)); // outputs 0.1
System.out.println(new BigDecimal("12.99").remainder(BigDecimal.ONE)); // outputs 0.99
If, as before, you want two decimal digits from this, you can do this:
System.out.println(new BigDecimal("12.1").remainder(BigDecimal.ONE).multiply(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP).intValue()); // outputs 0.1
System.out.println(new BigDecimal("12.99").remainder(BigDecimal.ONE).multiply(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP).intValue()); // outputs 0.99
Note that there a couple of differences between these last two methods and the first two: they preserve the sign of the argument, so if you use the final example for -12.99, you'll get -99 back, and they treat the fractional part of an integer as 1, so if you use the final example for 12, you'll get 100 back.
It's not necessary to use Number tyeps all the time. You can take advantage of String as a mediator.
String mediator = Double.valueOf(d1).toString();
mediator = mediator.substring(mediator.indexOf('.') + 1);
Integer result = Integer.valueOf(mediator);
Try this out
public static void main(String args[]){
double a=12.99;
double b=12.1;
System.out.println(method(a));
System.out.println(method(b));
}
private static int method(double a) {
return (int) ((a*100)%100);
}
sachin-pasalkar done it! little fix but fine!
public static int anyMethod(double a){
return (int) (a*100)%100;
}
check out this code returns digits after '.' always. Without any extra parameters other than double variable.
public int anyMethod(double d)
{
String numString = d+"";
return Integer.parseInt(numString.substring(numString.indexOf('.')+1));
}
If I understand correctly, you need to return n digits after the dot for a given double number. So... let's see:
public int decimalDigits(double x, int n) {
double ans;
ans = (x - (int) x) * Math.pow(10, n);
return (int) ans;
}
Done. Hope this helps you.
For your specific example, 'n = 2' should do.
Our application can get following numbers:
0.1
0.02
0.003
etc.
These values treated by our code as BigDecimal,as far we operate with money.
There is form on web UI, where user should view these floating parts of prices, transformed to following ones:
1
02
003
The question is,how to trim leading zero and delimiter character in input prices. Perhaps BigDecimal class has standard method something like trimLeadingZeroes(),but can't find any.
UPDATE:
trim just leading zero and delimiter symbol
For instance:
1 is 0.1
27 is 0.27
Something like this?
public String getDecimalFractions(BigDecimal value) {
String strValue = value.toPlainString();
int index = strValue.indexOf(".");
if(index != -1) {
return strValue.substring(index+1, strValue.length());
}
return "0";
}
Have you tried calling BigDecimal.unscaledValue? The downside is that 0.13 would then be 13 whereas you possibly want 1.3... it's slightly hard to tell. If you could give more examples, that would really help.
(That approach would also fail if the value were 1000 to start with - you'd end up with 1...)
Could it be something as simple as doing this:
public static BigDecimal fix(String str){
return new BigDecimal("0." + str);
}
so if you make
public static void main(String[] args) {
System.out.println(fix("1"));
System.out.println(fix("02"));
System.out.println(fix("003"));
}
It will print
0.1
0.02
0.003
when ever you have to deal with splitting something its a good bet Strings can be used for it.
You first just convert the bigdecimal into a string
String s=bd.toPlainString();
Then you simply split it as so
String s2=s.split("\\.")[1];
now String s2 contains the numbers after the delimiter
Conversion from BigDecimal to String:
import java.math.BigDecimal;
public class XXX {
public static void main(String[] args){
doIt("123");
doIt("123.1");
doIt("123.01");
doIt("123.0123");
doIt("123.00123");
}
static void doIt(String input){
BigDecimal bdIn = new BigDecimal(input);
System.out.println(bdIn+" -> "+convert(bdIn));
}
static String convert(BigDecimal bdIn) {
BigDecimal bdOut = bdIn.subtract(bdIn.setScale(0, BigDecimal.ROUND_DOWN));
return bdOut.signum() == 0 ? "0" : bdOut.toPlainString().substring(2);
}
}
Results are:
123 -> 0
123.1 -> 1
123.01 -> 01
123.0123 -> 0123
123.00123 -> 00123
The code works directly with any number and takes into account only the fractional part.
It also handles "0.0" gracefully.
Is this the conversion you wanted?
Here is another simple way of doing this - assuming your input is 1.023456
BigDecimal bd = new BigDecimal("1.023456");
BigInteger bi = bd.toBigInteger();
BigDecimal bd2 = bd.subtract(new BigDecimal(bi));
String afterDecimalPoint = bd2.scale() > 0 ?
bd2.toString().substring(2) : "";
This will give the exact result as you were looking for in bd3, i.e. it'll be 023456 for the above example.
It'll work ok for whole numbers too, due to the condition in last line, i.e. 1 will return ""
You could use the string representation of value (a BigDecimal) and StringUtils.substringAfter to do this:
StringUtils.substringAfter(value.toPlainString(), ".")
How about writing an extension method to extend this type. Simple method might multiply number until > 1
public int trimLeadingZeroes(bigint num) {
while (num < 1)
{
num = num * 10;
}
return num;
}
import java.math.BigDecimal;
import java.math.RoundingMode;
public class RemoveZeroes {
static final int SCALE = 10; // decimal range 0.1 ... 0.0000000001
static final String PADDING = "0000000000"; // SCALE number of zeroes
public static void main(String [] arg) {
BigDecimal [] testArray = {
new BigDecimal(0.27),
new BigDecimal(0.1),
new BigDecimal(0.02),
new BigDecimal(0.003),
new BigDecimal(0.0000000001),
};
for (int i = 0; i < testArray.length; i++) {
// normalize to the same scale
BigDecimal b = testArray[i].setScale(SCALE, RoundingMode.FLOOR);
// pad on the left with SCALE number of zeroes
String step1 = PADDING + b.unscaledValue().toString();
// remove extra zeroes from the left
String step2 = step1.substring(step1.length() - SCALE);
// remove extra zeroes from the right
String step3 = step2.replaceAll("0+$", "");
// print result
System.out.println(step3);
}
}
}
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