WatchGuard is currently running a beta program for our new macOS Host Sensor for our Threat Detection and Response service. We're looking for more macOS users to help provide insight and feedback to help us make the product even better. Please sign up for our beta program today!

It seems we have a bit of a language barrier. Maybe another voice will help?

x= (int)((a+b)*(value *(a-b)/100))

here a+b

is coming as intergervalue *(a-b)/100)

is decimalso

the second part is discarded as i am trypecasting to intBecause all of your variables are integers, all values, including the intermediate values will have an integer type. When you divide two values, the result is an integer. In your example above you divide by 100. If the divisor is between 1 and 99, you get the same result, 0. In effect, 1/100 = 99/100, for the purpose of this calculation as both result in integer 0.

To capture the fractional part of the calculation you need to determine how many decimal places you need. It's probably beyond the scope of your project to use true floating point (variable precision), so decide how many digits to the right of the decimal that you'll need. Are 2 enough? With your equation that becomes the simplest. Are 3 enough? That resolution is 1/1000.

x= (int) ((a+b)*(value *(a-b)/100))

becomes

2 decimal places -- x = ((a+b) * (value *(a-b)))

3 decimal places -- x = ((a+b) * 1000 * (value *(a-b)/100))

When you convert the value to a string, insert a decimal point 2 (or 3 places) from the right.

If you need the fractional part for future computations, just subtract it. But remember that you're using integer data types so the value is 1000 times the fraction's true value.

// Compute value * 1000

x = ((a+b) * 1000 * (value *(a-b)/100))

// Compute fractional part

frac = x % 1000;

// Restore x to the correct scale

x /= 1000;

Good Luck,

Kent