In: Computer Science
Complete the following code segment that is intended to extract the Dirham and fils values from a price given as a floating-point value; for example, a price 2.95 yields values 2 and 95 for the dirham and fils. as per the following description:
Assign the price to an integer variable named dirham to get the dirhmas part of the price
Subtract dirham from price then multiply the difference by 100 and add 0.5
Assign the result to an integer variable named fils
Display both of dirham and fils values each in a separate line
CODE:
class di_fi
{
public static void main(String[] args)
{
// Initializing the price
float price = 2.95f;
// dirham part of the price
//to do so typecast it to integer and assigning to an integer
int dirham = (int)price;
// fils part , subtracting dirham from price
// then multiplying difference by 100 and adding 0.5
int fils = (int) ((int)((price - dirham) * 100) + 0.5);
// Displaying the result
System.out.println(dirham + " dirham");
System.out.println(fils + " fils");
}
}
output:
IF YOU HAVE ANY DOUBT PLEASE LEAVE A COMMENT.