Define a class, put your code into a method there
You want to call this from various places. So we certainly do not want to be copy-pasting this code into various places. Let's create a class with a method to do the calculations.
package work.basil.demo;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class RogZidPosWolTop
{
// Name your magic numbers. I do not know their meaning, so I invented x and y factor as the names. You can do better.
static final private BigDecimal XFACTOR = new BigDecimal( "9.478" );
static final private BigDecimal YFACTOR = new BigDecimal( "145" );
public BigDecimal calculateTopFromRog ( final BigDecimal rog )
{
// `divideAndRemainder` returns a two-element BigDecimal array containing the result of divideToIntegralValue followed by the result of remainder on the two operands.
BigDecimal[] zids = rog.divideAndRemainder( BigDecimal.ONE );
BigDecimal zid = zids[ 0 ]; // integer portion of rog.
BigDecimal zidLast = zids[ 1 ]; // fractional portion of rog.
BigDecimal pos = zidLast.multiply( RogZidPosWolTop.XFACTOR );
if ( pos.scale() == 0 )
{
pos = pos.setScale( 2 , RoundingMode.CEILING ); // Why set the scale when it is zero? I do not understand your logic here.
}
BigDecimal[] tops = pos.divideAndRemainder( BigDecimal.ONE );
BigDecimal top = zids[ 0 ]; // integer portion of pos.
BigDecimal topLast = zids[ 1 ]; // fractional portion of pos.
BigDecimal result = topLast.multiply( RogZidPosWolTop.YFACTOR );
return result;
}
}
Example usage:
RogZidPosWolTop rogZidPosWolTop = new RogZidPosWolTop();
BigDecimal x = rogZidPosWolTop.calculateTopFromRog( new BigDecimal( "42.7" ) );
System.out.println( "x = " + x );
x = 101.5
If you do not understand the basics of object-oriented programming such as classes, methods, and instances, you need more study. Read the Java Tutorials by Oracle as a starter, free-of-cost online. And read any number of good books such as Head First Java (outdated but may still be good for the basics).
Smart objects rather than dumb strings
I rewrote your code. You are thinking in terms of string manipulation. This is not a wise way to handle numbers.
Better to use smart objects rather than dumb strings. A BigDecimal
is smart, it knows how to do math. It offers methods to get the integer port and the fractional part. So learn to use those methods. Notice that I make no use of String
or text at all in this rewritten code.
You seem to be chopping strings as an awkward way to get the integer and fractional portions of the number. Instead, I call BigDecimal::divideAndRemainder
to divide by one. This returns an array of two elements. The first is a BigDecimal
with the integer value. The second is the fractional amount, equivalent to if we had subtract the integer value from our original number. See this Question, How to get integer and fraction portions of a BigDecimal in Java.
BigDecimal[] zids = new BigDecimal( "42.7" ).divideAndRemainder( BigDecimal.ONE );
System.out.println( Arrays.toString( zids ) );
[42.0, 0.7]
Notice that if the input is a negative number, both the integer and the fraction will be negative as well when using divideAndRemainder
; I cannot know if this is appropriate to your problem or not. If not, take the absolute value of the fractional portion.
You have not explained your logic or details. You gave no example inputs-outputs. And you did not translate your naming to English. So I can only guess if I got this right. But it should, at least, get you going in a better direction.
// Name your magic numbers. I do not know their meaning, so I invented x and y factor. You can do better.
BigDecimal xFactor = new BigDecimal( "9.478" );
BigDecimal yFactor = new BigDecimal( "145" );
// Input
BigDecimal rog = new BigDecimal( "42.7" ); // Example data. Guessing as to the type of your `rog` variable, not stated in the Question.
// `divideAndRemainder` returns a two-element BigDecimal array containing the result of divideToIntegralValue followed by the result of remainder on the two operands.
BigDecimal[] zids = rog.divideAndRemainder( BigDecimal.ONE );
BigDecimal zid = zids[ 0 ]; // integer portion of rog.
BigDecimal zidLast = zids[ 1 ]; // fractional portion of rog.
BigDecimal pos = zidLast.multiply( xFactor );
if ( pos.scale() == 0 )
{
pos = pos.setScale( 2 , RoundingMode.CEILING ); // Why set the scale when it is zero? I do not understand your logic here.
}
BigDecimal[] tops = pos.divideAndRemainder( BigDecimal.ONE );
BigDecimal top = zids[ 0 ]; // integer portion of pos.
BigDecimal topLast = zids[ 1 ]; // fractional portion of pos.
BigDecimal result = topLast.multiply( yFactor );