ในบทความนี้ เราจะเข้าใจวิธีการคูณเลขทศนิยมสองตัว ตัวเลขทศนิยมคือตัวเลขที่มีค่าทศนิยม ชนิดข้อมูลแบบโฟลตเป็นจุดทศนิยม IEEE 754 แบบ 32 บิตที่มีความแม่นยำเพียงจุดเดียว ส่วนใหญ่จะใช้เพื่อบันทึกหน่วยความจำในอาร์เรย์ขนาดใหญ่ของตัวเลขทศนิยม ค่าเริ่มต้นคือ 0.0f ไม่เคยใช้ประเภทข้อมูลแบบ Float สำหรับค่าที่แม่นยำ เช่น สกุลเงิน
ด้านล่างนี้เป็นการสาธิตสิ่งเดียวกัน -
ป้อนข้อมูล
สมมติว่าข้อมูลที่เราป้อนคือ −
Value_1: 12.4f Value_2: 15.7f
ผลผลิต
ผลลัพธ์ที่ต้องการจะเป็น −
Result : 194.68f
อัลกอริทึม
Step 1- Start Step 2- Declare three floating points: input_1, input_2 and product Step 3- Prompt the user to enter two floating point value/ define the floating-point values Step 4- Read the values Step 5- Multiply the two values using a multiplication operator (*) Step 6- Display the result Step 7- Stop
ตัวอย่างที่ 1
ที่นี่ ผู้ใช้ป้อนอินพุตตามข้อความแจ้ง คุณสามารถลองใช้ตัวอย่างนี้ในเครื่องมือกราวด์ของเรา
.
import java.util.Scanner;
public class MultiplyFloatValues{
public static void main(String[] args){
float input_1, input_2, my_prod;
Scanner my_scan = new Scanner(System.in);
System.out.println("A reader object has been defined ");
System.out.println("Enter the first floating point number: ");
input_1 = my_scan.nextFloat();
System.out.println("Enter the second floating point number: ");
input_2 = my_scan.nextFloat();
my_prod = input_1 * input_2;
System.out.println("\nThe product of the two values is: " + my_prod);
}
} ผลลัพธ์
A reader object has been defined Enter the first floating point number: 12.4 Enter the second floating point number: 15.7 The product of the two values is: 194.68
ตัวอย่างที่ 2
ในที่นี้ ได้มีการกำหนดจำนวนเต็มไว้แล้ว และค่าของมันถูกเข้าถึงและแสดงบนคอนโซล
public class MultiplyFloatValues{
public static void main(String[] args){
float value_1, value_2, my_prod;
value_1 = 12.4f;
value_2 = 15.7f;
System.out.printf("The two numbers are %.2f and %.2f ",value_1, value_2 );
my_prod = value_1 * value_2;
System.out.println("\nThe product of the two values are: " + my_prod);
}
} ผลลัพธ์
The two numbers are 12.40 and 15.70 The product of the two values are: 194.68