How do I steer manually?

I would like to bypass manually without the round () method. Therefore, I can tell my program that my number, at this moment, I want you to come together. Let me give you some examples:
Input number: 144
Introductory rounding: 2
Output Rounded Number: 140

Input number: 123456
Introductory rounding: 3
Derived rounded number: 123500

And how an additional add-on can go behind a comma: Enter the number: 123.456
Introductory rounding: -1
Derived rounded number: 123.460

I do not know how to start programming ... Someone tell me how I can start with this problem?

Thanks for helping me :)

I would like to know the best programming, so I do not want to use the round and make my own, so that I can understand it better :)

+4
source share
5 answers

A simple way to do this:

  • Divide the number by ten.
  • Disassemble it in any way you want.
  • Multiply the result by the same power of ten in step 1

Let me show you an example:

You want to round the number 1234.567 to two decimal places (the desired result is 1234.57 ).

 x = 1234.567; p = 2; x = x * pow(10, p); // x = 123456.7 x = floor(x + 0.5); // x = floor(123456.7 + 0.5) = floor(123457.2) = 123457 x = x / pow(10,p); // x = 1234.57 return x; 

Of course, you can compress all of these steps in one. I took it step by step to show you how it works. In a compact java form, it will be something like:

 public double roundItTheHardWay(double x, int p) { return ((double) Math.floor(x * pow(10,p) + 0.5)) / pow(10,p); } 

As for whole positions, you can easily check that this also works (with p < 0 ).

Hope this helps

+1
source

if you need advice on how to get started,

  • write down the calculations step by step what you need to do to get from 144.2 β†’ 140
  • replace your math with java commands, this should be easy, but if you have problems just look here and here
+1
source
 public static int round (int input, int places) { int factor = (int)java.lang.Math.pow(10, places); return (input / factor) * factor; } 

Basically, this is what it is dividing the input by your factor, and then multiplying again. When dividing integers into languages ​​such as Java, the rest of the division is discarded from the results.

edit: the code was faulty, fixed it. Also, java.lang.Math.pow is that you get 10 to the nth power, where n is the value of places . In the OP example, the number of places to consider is increased by one.

Rename: as indicated in the comments, the above will give you the word, i.e. the result of rounding down . If you do not want to always round down, you should also save the module in another variable. Like this:

 int mod = input % factor; 

If you want to always get the ceiling, i.e. round up , check if mod is equal. If so, leave it to that. Otherwise, add factor to the result.

 int ceil = input + (mod == 0 ? 0 : factor); 

If you want to round to the nearest, then get the word if mod less than factor / 2, or the ceiling otherwise.

+1
source

Divide (positive) / multiply (negative) by the β€œrounding time” 10 - 1 (144 / (10 * (2 - 1)). This will give you the same thing in this case. The last digit (4). Determine if there are more it is either equal to 5 (less). Make it equal to 0 or add 10, depending on the previous answer. Multiply / Divide it by "input rounding time" of 10 - 1. This should give you your value.

If it's for homework. The goal is to teach you how to think about yourself. I may have given you the answer, but you still need to write the code yourself.

Next time you should write your own code and ask what is wrong.

0
source

For integers, one way would be to use a combination of the mod operator, which is the percent symbol%, and the division operator. In the first example, you should calculate 144% 10, the result will be 4. And calculate 144/10, which gives 14 (as an integer). You can compare the result of the mod operation with half the denominator to find out if you should bypass 14 to 15 or not (in this case not), and then multiply back by the denominator to get the answer.

In the psuedo code, assuming n is a number rounded, p is a power of 10 representing the position of the significant digits:

 denom = power(10, p) remainder = n % denom dividend = n / denom if (remainder < denom/2) return dividend * denom else return (dividend + 1) * denom 
0
source

Source: https://habr.com/ru/post/1479597/


All Articles