0

Right now I'm using something like this: Basically the program is supposed to print X (right most digit of a #) to X decimal places for example:

  • entered 3.56, should display 0000000000000003.560
  • entered 56.7 should display: 000000000056.700000
  • entering 1002.5 should display 00000000000001002.50

but number % 10,condition right now only accepts number w/o decimals, so the program closes if i enter a number with decimals I only need an alternative for number % 10.

double number;
if (number % 10 == 1)
System.out.printf("%020.1f\n",number);
Steve P.
  • 14,489
  • 8
  • 42
  • 72
Brian Do
  • 23
  • 1
  • 5
  • 1
    I don't understand what it is supposed to do. Printing a digit to that number of decimal places makes no sense, and doesn't match your examples. I don't think you are using the words correctly. – Robin Green Nov 01 '13 at 23:38
  • 3
    Your title says "the right most digit of an integer", but `number` is a `double`. What exactly do you need? – ajb Nov 01 '13 at 23:40
  • 3
    I "see the description". I still have no clue what it means. – ajb Nov 01 '13 at 23:44
  • Why in the world would you want those outputs in the first place? – Paul Samsotha Nov 01 '13 at 23:46
  • Sorry, I was having difficulty being able to post part the code. – Brian Do Nov 01 '13 at 23:47
  • What do `000000000056.7000000` and `0000000000000003.560 ` have in common in relation to `3.56` and `56.7`? I don't see it. – Paul Samsotha Nov 01 '13 at 23:48
  • I need the outputs for a project, my only problem is finding an alternative to (number % 10). – Brian Do Nov 01 '13 at 23:48
  • 1
    ok so you see 56.7 (6 is the right most digit integer in the number) so 000000000056.700000 <- has 6 decimal places. With a field width of 20. in (3.56) 3 is the right most integer digit, so 0000000000000003.560 <- has 3 decimal places – Brian Do Nov 01 '13 at 23:50
  • Why do you want to not use `%`? Is it forbidden to use this, or is it just not working the way you want? If it's the second one, try saying `(int)number % 10`, to convert the double to an integer (and throw away all the digits to the right of the decimal). – ajb Nov 01 '13 at 23:53
  • yes, now i need to find another way besides (number % 10); because my program only displays numbers w/o decimals. – Brian Do Nov 01 '13 at 23:53
  • I need the numbers after the decimals – Brian Do Nov 01 '13 at 23:53
  • You'd store the result of `(int)number % 10` in a separate variable to determine the number of decimal places you need, and use that variable in your `printf` statement. – Josh Nov 01 '13 at 23:55
  • another example is if someone entered: 1034545.2132 it should display 00000001034545.21320 ex2. if entered 10568.25 it should display 00000010568.25000000 – Brian Do Nov 01 '13 at 23:55
  • If this is a response to me, "throw away the digits to the right of the decimal" doesn't mean throw them away permanently. `(int)number` will return `number` with all the digits to the right of the decimal thrown away, but this won't change the value of `number` unless you assign `number = (int)number` or something. – ajb Nov 01 '13 at 23:55
  • In your previous code example you ware using `%10` but in title you are saying `without using number % 10`. So can you use `%10` or not? – Pshemo Nov 01 '13 at 23:57
  • @Brian DO, What do you want this condition to check for `if (number % 10 == 1)`. It is unclear your intention. – Paul Samsotha Nov 01 '13 at 23:58
  • `"another example is if someone entered: 1034545.2132 it should display 00000001034545.21320 ex2. if entered 10568.25 it should display 00000010568.25000000"`. This still makes absolutely no sense. – Paul Samsotha Nov 01 '13 at 23:59
  • basically (number % 10 == 1) means, if the right most integer digit of a number is 1 make the output display only 1 decimal place example= 341.3332444 it should display 341.3 – Brian Do Nov 01 '13 at 23:59
  • if the right most integer digit of a number is 4 (number %10) make the output display 4 decimal places example entered: 5674.3443122123 it should display only 5674.3443 – Brian Do Nov 02 '13 at 00:00

4 Answers4

1

If I have interpreted your question correctly then this looks like it does what you ask:

public void test() {
  strangePrint(3.1415);
  strangePrint(2.0);
  strangePrint(2.1);
  strangePrint(2.2);
  strangePrint(2.999);
  strangePrint(37.4);
  strangePrint(3.56);
  strangePrint(56.7);
  strangePrint(1002.5);
}

private void strangePrint(double d) {
  // Get the integer part
  int n = (int)d;
  // The last digit of the integer defines the decimal places.
  int digits = n%10;
  System.out.printf("%020."+digits+"f\n", d);
}

prints

0000000000000003.142
00000000000000002.00
00000000000000002.10
00000000000000002.20
00000000000000003.00
000000000037.4000000
0000000000000003.560
0000000000056.700000
00000000000001002.50
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
1

It seems that you are looking for something like

System.out.printf("%020." + ((int) number) % 10 + "f\n", number);

((int) number) will get rid of fraction making 56.7 -> 56, so now you can safely use %10 to get last digit.

DEMO

Pshemo
  • 122,468
  • 25
  • 185
  • 269
0

From number in format:

ABCDEX.FGHI

you can extract X by:

int x = (int) original; //get rid of what is after the decimal point
//now x is ABCDEX
x = x % 10;
//now x is X

now you can join this int with string to create pattern for printf.

kajacx
  • 12,361
  • 5
  • 43
  • 70
0

Based off of your original post, it seemed like you weren't allowed to use mod, so here's how I would do it:

private void transform(Double number)
{
    int result;
    int x = number.intValue();

    if (x < 10)
    {
        result = x;
    }
    else
    {
        Double y = x / 10.0;
        int z = y.intValue();
        result = x-10*z;
    }

    System.out.printf("%020." + result + "f\n", number);
}

Test runs:

transform(3.56);
transform(56.7);
transform(1002.5);  

Prints:

0000000000000003.560
0000000000056.700000
00000000000001002.50

EDIT:
If I misinterpreted and you are allowed to use mod, then the answer is simply:

private void transform(Double number)
{ 
     System.out.printf("%020." + ((int) number) % 10 + "f\n", number);
}

as others have suggested. Sorry if I misunderstood.

Steve P.
  • 14,489
  • 8
  • 42
  • 72