Tutorial :Java using Mod floats


I am working on an exercise in Java. I am supposed to use / and % to extract digits from a number. The number would be something like 1349.9431. The output would be something like:

1349.9431  1349.943  1349.94  1349.9  

I know this is a strange way to do but the lab exercise requires it.


Let's think about what you know. Let say you have the number 12345. What's the result of dividing 12345 by 10? What's the result of taking 12345 mod 10?

Now think about 0.12345. What's the result of multiplying that by 10? What's the result of that mod 10?

The key is in those answers.


if x is your number you should be able to do something like x - x%0.1 to get the 1349.9, then x - x%.0.01 to get 1349.94 and so on. I'm not sure though, doing mod on floats is kind of unusual to begin with, but I think it should work that way.

x - x%10 would definetly get you 1340 and x - x%100 = 1300 for sure.


Well the work will be done in background anyway, so why even bother, just print it.

float dv = 1349.9431f;  System.out.printf("%8.3f %8.2f %8.1f", dv, dv, dv);  

Alternatively this could be archived with:

float dv = 1349.9431f;  System.out.println(String.format("%8.3f %8.2f %8.1f", dv, dv, dv));  


This is a homework question so doing something the way you would actually do in the real world (i.e. using the format method of String as Margus did) isn't allowed. I can see three constraints on any answer given what is contained in your question (if these aren't actually constraints you need to reword your question!)

  1. Must accept a float as an input (and, if possible, use floats exclusively)
  2. Must use the remainder (%) and division (/) operator
  3. Input float must be able to have four digits before and after the decimal point and still give the correct answer.

Constraint 1. is a total pain because you're going to hit your head on floating point precision quite easily if you have to use a number with four digits before and after the decimal point.

float inputNumber = 1234.5678f;  System.out.println(inputNumber % 0.1);  

prints "0.06774902343743147"

casting the input float to a double casuses more headaches:

float one = 1234.5678f;  double two = (double) one;  

prints "1234.5677490234375" (note: rounding off the answer will get you 1234.5677, which != 1234.5678)

To be honest, this had me really stumped, I spent way too much time trying to figure out how to get around the precision issue. I couldn't find a way to make this program work for 1234.5678f, but it does work for the asker's value of 1349.9431f.

float input = 1349.9431f;    float inputCopy = input;  int numberOfDecimalPoints = 0;  while(inputCopy != (int) inputCopy)  {    inputCopy = inputCopy * 10;    numberOfDecimalPoints++;  }    double inputDouble = (double) input;    double test = inputDouble * Math.pow(10, numberOfDecimalPoints);  long inputLong = Math.round(test);    System.out.println(input);  for(int divisor = 10; divisor < Math.pow(10, numberOfDecimalPoints); divisor = divisor * 10)  {    long printMe = inputLong - (inputLong % divisor);    System.out.println(printMe / Math.pow(10, numberOfDecimalPoints));  }    

Of my three constraints, I've satisfied 1 (kind of), 2 but not 3 as it is highly value-dependent.

I'm very interested to see what other SO people can come up with. If the asker has parsed the instructions correctly, it's a very poor exercise, IMO.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »