Numeric Value থেকে BigInteger এবং BigDecimal তৈরি করা

String এবং Numeric Conversion - জাভা ম্যাথ প্যাকেজ (Java.math Package) - Java Technologies

288

java.time প্যাকেজ মূলত তারিখ ও সময় সম্পর্কিত ক্লাসের জন্য ব্যবহৃত হলেও, BigInteger এবং BigDecimal এর মতো সংখ্যা সম্পর্কিত ক্যালকুলেশন করার জন্য java.math প্যাকেজে রয়েছে। এগুলি arbitrary precision (অগণিত সঠিকতা) সংখ্যার জন্য ব্যবহৃত হয়, যা সাধারণ int বা long এর সীমার বাইরে থাকা সংখ্যাগুলি পরিচালনা করতে সক্ষম।

যখন আপনি Numeric Value (যেমন, পূর্ণসংখ্যা বা দশমিক সংখ্যা) থেকে BigInteger বা BigDecimal তৈরি করতে চান, তখন BigInteger এবং BigDecimal ক্লাসে বিভিন্ন কনস্ট্রাক্টর এবং মেথড আছে।

1. BigInteger থেকে Numeric Value তৈরি করা:

BigInteger হল একটি ক্লাস যা arbitrary-precision integers পরিচালনা করতে সক্ষম। এটি সাধারণ int বা long এর সীমার বাইরে থাকা সংখ্যাগুলি ধারণ করতে পারে।

BigInteger তৈরি করা Numeric Value থেকে:

import java.math.BigInteger;

public class BigIntegerExample {
    public static void main(String[] args) {
        // Create a BigInteger object from a string representation of a large number
        BigInteger bigIntegerFromString = new BigInteger("1234567890123456789012345678901234567890");
        System.out.println("BigInteger from String: " + bigIntegerFromString);

        // Create a BigInteger object from a long value
        long longValue = 1234567890L;
        BigInteger bigIntegerFromLong = BigInteger.valueOf(longValue);
        System.out.println("BigInteger from long: " + bigIntegerFromLong);

        // Create a BigInteger object from an int value
        int intValue = 12345;
        BigInteger bigIntegerFromInt = BigInteger.valueOf(intValue);
        System.out.println("BigInteger from int: " + bigIntegerFromInt);
    }
}

Output:

BigInteger from String: 1234567890123456789012345678901234567890
BigInteger from long: 1234567890
BigInteger from int: 12345

ব্যাখ্যা:

  • new BigInteger(String): এখানে একটি বড় সংখ্যার স্ট্রিং থেকে BigInteger তৈরি করা হয়েছে।
  • BigInteger.valueOf(long): এখানে long টাইপের মান থেকে BigInteger তৈরি করা হয়েছে।

2. BigDecimal থেকে Numeric Value তৈরি করা:

BigDecimal হল একটি ক্লাস যা arbitrary-precision decimal numbers (ডেসিমাল সংখ্যা) পরিচালনা করতে সক্ষম। এটি গাণিতিক হিসাবের জন্য এবং আর্থিক হিসাবের জন্য আদর্শ, যেখানে নির্ভুল দশমিক সঠিকতা প্রয়োজন।

BigDecimal তৈরি করা Numeric Value থেকে:

import java.math.BigDecimal;

public class BigDecimalExample {
    public static void main(String[] args) {
        // Create a BigDecimal object from a string representation of a decimal number
        BigDecimal bigDecimalFromString = new BigDecimal("12345.6789");
        System.out.println("BigDecimal from String: " + bigDecimalFromString);

        // Create a BigDecimal object from a double value
        double doubleValue = 12345.6789;
        BigDecimal bigDecimalFromDouble = BigDecimal.valueOf(doubleValue);
        System.out.println("BigDecimal from double: " + bigDecimalFromDouble);

        // Create a BigDecimal object from an int value
        int intValue = 100;
        BigDecimal bigDecimalFromInt = new BigDecimal(intValue);
        System.out.println("BigDecimal from int: " + bigDecimalFromInt);
    }
}

Output:

BigDecimal from String: 12345.6789
BigDecimal from double: 12345.6789
BigDecimal from int: 100

ব্যাখ্যা:

  • new BigDecimal(String): এখানে String থেকে BigDecimal তৈরি করা হয়েছে, যা ডেসিমাল সংখ্যার সঠিকতা বজায় রাখে।
  • BigDecimal.valueOf(double): এখানে double টাইপের মান থেকে BigDecimal তৈরি করা হয়েছে।
  • new BigDecimal(int): এখানে int টাইপের মান থেকে BigDecimal তৈরি করা হয়েছে।

3. BigInteger এবং BigDecimal এর মধ্যে Conversion

BigInteger থেকে BigDecimal এ রূপান্তর:

import java.math.BigInteger;
import java.math.BigDecimal;

public class ConversionExample {
    public static void main(String[] args) {
        // Create BigInteger object
        BigInteger bigInt = new BigInteger("123456789123456789");
        
        // Convert BigInteger to BigDecimal
        BigDecimal bigDecimal = new BigDecimal(bigInt);
        
        System.out.println("BigInteger: " + bigInt);
        System.out.println("Converted to BigDecimal: " + bigDecimal);
    }
}

Output:

BigInteger: 123456789123456789
Converted to BigDecimal: 123456789123456789

BigDecimal থেকে BigInteger এ রূপান্তর:

import java.math.BigDecimal;
import java.math.BigInteger;

public class ConversionExample {
    public static void main(String[] args) {
        // Create BigDecimal object
        BigDecimal bigDecimal = new BigDecimal("12345.6789");
        
        // Convert BigDecimal to BigInteger (removes decimal part)
        BigInteger bigInt = bigDecimal.toBigInteger();
        
        System.out.println("BigDecimal: " + bigDecimal);
        System.out.println("Converted to BigInteger: " + bigInt);
    }
}

Output:

BigDecimal: 12345.6789
Converted to BigInteger: 12345

ব্যাখ্যা:

  • BigInteger(BigDecimal): BigInteger থেকে BigDecimal এ রূপান্তরের জন্য new BigDecimal(BigInteger) ব্যবহার করা হয়।
  • toBigInteger(): BigDecimal থেকে BigInteger এ রূপান্তরের জন্য toBigInteger() মেথড ব্যবহার করা হয়, যা ডেসিমাল অংশটি বাদ দিয়ে পূর্ণসংখ্যা প্রদান করে।

  • BigInteger এবং BigDecimal উভয়ই arbitrary precision numbers পরিচালনা করার জন্য ব্যবহৃত হয়, তবে তারা integer এবং decimal সংখ্যার জন্য আলাদা আলাদা ক্লাস।
  • BigInteger তৈরি করতে String, long, অথবা int টাইপ থেকে মান ব্যবহার করা যায়।
  • BigDecimal তৈরি করতে String, double, বা int থেকে মান ব্যবহার করা যায়।
  • BigInteger এবং BigDecimal এর মধ্যে রূপান্তর করা সম্ভব, তবে BigDecimal এর ডেসিমাল অংশ মুছে ফেলতে BigInteger রূপান্তর করা হয়।

এভাবে BigInteger এবং BigDecimal এর মধ্যে রূপান্তর এবং তাদের বিভিন্ন ব্যবহারের মাধ্যমে আপনি বড় সংখ্যাগুলির সাথে কাজ করতে পারেন।

Content added By
Promotion

Are you sure to start over?

Loading...