round up number in Android - android

I wish to round up a number in android application with coding as shown below.
float num1= Float.parseFloat(n1.getText().toString());
float num2= Float.parseFloat(n2.getText().toString());
float outc = num1/input;
int final1;
The application getting user input for num1 and num2, and perform to find outc. I wish to rounding up the outc to become an integer final1 where like below:
if outc = 3.8 , final1= 4
if outc = 8.01 , final1= 9
if outc = 12.21 , final1= 13
if outc = 20.45 , final1= 21
*only involve positive value
I try to search for solution, and found that mostly they are normal rounding up solution like this.
It that anyway to perform round up as I mentioned?
Thank you.

Math.ceil()
int result = (int) Math.ceil(outc);
BTW, you can't name variable final, it is reserved word.

Use the ceil function described in:
https://developer.android.com/reference/java/lang/Math.html#ceil(double)

Related

Color difference atan and atan2 difference in results

EDIT: obviously I've made ann error in atan2 after many different approaches, but still curious to find formulas.
I've read multiple topics and articles, yet do not understand why atan and atan2 give different results after convertion into degrees. Here is my example (from CIElab colour space) x -79.7751, y 2.677374209. Calculation without any changes to code will give incorrect value in both cases:
atan(-79.7751 / 2.677374209) = -1,537 / or in degrees -88,07778762
atan2(-79.7751 , 2.677374209) = 3,1258 / or in degrees 179,0965
However, that example is from an article so we can check results. Basically answer should be 271.9222. I've found over the internet correction for atan, now I've got same results, yet I still have indeterminate value for x=y=0 in atan, hence some corrections should be made for atan2. Unfortunately I did not found any that would yield same result and and would be proven (I mean what could be found in literature). Most sources for CIElab say to use atan2 without any corrections and as you can below it is not correct in my particular case. Here is what I got on this:
atan(-79.7751 / 2.677374209) = -1,537 / for atan < 0 : atan + 2pi
hence atan = 4,7459 / or in degrees 271,922 ( BINGO! )
atan2(-79.7751 , 2.677374209) = ̶3̶,̶1̶2̶5̶8̶ 3,108 / or in degrees ̶1̶7̶9̶,̶0̶9̶6̶5̶ 178,0777876
now convert, for deg > 90 : 450 - deg
atan2 result 271,922
̶A̶s̶ ̶y̶o̶u̶ ̶c̶a̶n̶ ̶s̶e̶e̶,̶ ̶̶c̶l̶o̶s̶e̶ ̶b̶u̶t̶ ̶n̶o̶ ̶c̶i̶g̶a̶r̶̶.̶ ̶A̶l̶s̶o̶ ̶i̶t̶ ̶d̶i̶f̶f̶e̶r̶s̶ ̶b̶y̶ ̶1̶ ̶d̶e̶g̶r̶e̶e̶ ̶a̶n̶d̶ ̶s̶o̶m̶e̶ ̶c̶h̶a̶n̶g̶e̶.̶ My question is how exactly I can make a correction of atan2 to get proper results?
Your formulas may be wrong, depending on the library you use. See the C++ std for atan and atan2:
atan(y/x) returns in range {-pi/2, pi/2}
atan2(y,x) returns in range {0, 2pi}
You have exchanged x,y in both cases. This has the effect of this figure:

How to mask EditText to take input in specific format

I'm trying to restrict what my EditText takes as input. I want it to take a maximum of 21 million (20,999,999.99999999). Is it possible to achieve> if, then how do I go about it? I've tried using InputFilter with a pattern [0-9]{0,9}+((\.[0-9]{0,7})?)||(\.)?. Show me how to do it.
Well, I read Francesco's answer and your comment also. If you need to check the decimal point, you can try this below.
EditText someText = findViewById(R.id.someValueInputHi);
String paw = someText.getText().toString().replaceAll("\\s+", ""); //rem unusual spaces
Double someInput = Double.parseDouble(paw);
if (someInput < 5.3465726543d | someInput > 3.7834657836d) {
//Ok now
} else {
//Oops, no man
}
You only take the value between 5.3465726543 and 3.7834657836.
Why double?
As the name implies, a double has 2x the precision of float. In general, a double has 15 decimal digits of precision, while float has 7.
EDIT:
Francesco's answer is now deleted.

Template Matching for android-opencv application

I have developed an algorithm for android using OpenCV. I need to find overlap between the previous image and current frame. So, I have produced the template from previous image to match with current frame to make a photograph. It is the procedure to complete photographing. (Taking more than 10 picture)
Here is the code that I have developed to find the overlap.
public void overlapFinder(Mat inputFrame , Mat inputTemplate )
{
Mat mResult;
int resultWidth = inputFrame.width() - inputTemplate.width() + 1;
int resultHeight = inputFrame.height() - inputTemplate.height() + 1;
mResult = new Mat(resultHeight, resultWidth, CvType.CV_8U);
Imgproc.matchTemplate(inputFrame, inputTemplate, mResult,Imgproc.TM_CCORR_NORMED) ;
Core.MinMaxLocResult result = Core.minMaxLoc(mResult);
#SuppressWarnings("unused")
double maxVal = result.maxVal;
}
The problem is that when the "overlap function" is called after generating template from previous image, the application is crashed.
Would anyone please help me with that?
Thanks
Maybe you really need to do some debugging first, but in any case, I can see from your code that it would be worthwhile checking the sizes of your images - it seems that your code assumes the template is always smaller than the input frame.
If that's not true, you will get negative resultWidth and/or resultHeight, which will make it crash.
One other thing - the documentation suggests that the result type should be CV_32FC1.
PS - Try initialising your result like this:
mResult.create(resultHeight, resultWidth, CvType.CV_32FC1);

Android calculator: manipulation query + remove unwanted zeros for the answer output

Hi I am working on an android calculator apps and the now working on the manipuations. I have defined for the following:
ArrayList<Float> inputnum = new ArrayList<Float>();
float inputnum1;
float inputnum2;
and then for the operations,
case MULTIPLY:
inputnum1 = inputnum.get(0);
inputnum2 = inputnum.get(1);
inputnum.add(inputnum1 * inputnum2);
Display.setText(String.format("%.9f", inputnum.get(0)));
similar for the division one.
The muliply function and divide function works well for integers (eg 5* 4 output 20.00000000)
however, when it deals with figures with decimal places, eg 5.3 * 4, it output as 21.12000089, which is incorrect.
what is the problem?
also, how to set output to Display to remove unnecessary zero? eg
when 5*4 it only show 20 instead of 20.000000 as final answer?
when 5.3*4 = 21.12 instead of 21.12000000 as final answer?
Thanks a lot!
Just to change all the related float to double will then avoid presenting the rounding error.
If wanted to present 9 decimal places by filling up zero after the dot, eg 7.56 become 7.560000000, can use the below coding.
Display.setText(String.format("%.9f", inputnum.get(0)));

How to use use cmyk color codes in android?

I have an doubt, I don't know how to use cmyk colors in android. If any one knows please help me. I am waiting for your valuable replies.
You just need a function, which converts the CMYK value to RGB? Or do you want to convert a whole image, which is CMYK?
For the first problem, as pseudocode rgb2cmyk:
int r,g,b,c,m,y,k;
int computedC,computedM,computedY;
int minCMY;
if(r==0 && g==0 && b==0) return {0,0,0,1}
computedC = 1 - (r/255);
computedM = 1 - (g/255);
computedY = 1 - (b/255);
minCMY = Math.min(computedC,Math.min(computedM,computedY));
computedC = (computedC - minCMY) / (1 - minCMY) ;
computedM = (computedM - minCMY) / (1 - minCMY) ;
computedY = (computedY - minCMY) / (1 - minCMY) ;
return {computedC,computedM,computedY,minCMY};
And for the other way around, just calculate it backwards :)
For the problem no. 2:
Its easier, 'cause there is a special tool called ColorSpace:
How do I convert images between CMYK and RGB in ColdFusion (Java)?
Hope that helps :3

Categories

Resources