Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,596 questions

55,330 answers

573 users

How to convert a decimal to a long in Java

1 Answer

0 votes
public class DecimalToLongProgram {

    /**
        ============================================================
        Convert decimal-like values to long in Java.

        This class demonstrates:
          • Conversion using Math.round() for floating-point rounding.
          • Conversion using a direct cast (long) for truncation.
          • Conversion using BigDecimal.longValue() and longValueExact().
          • A helper method that prints all conversion styles.

        Notes:
          • Math.round() returns a long and rounds to nearest integer.
          • (long)value truncates the fractional part.
          • BigDecimal.longValue() truncates.
          • BigDecimal.longValueExact() throws if fractional or out of range.
        ============================================================
    */

    // Rounds a double to long
    static long convertDoubleToLong(double value) {
        return Math.round(value);
    }

    // Truncates a double to long
    static long castDoubleToLong(double value) {
        return (long) value;
    }

    // Rounds or truncates a BigDecimal
    static long convertBigDecimalToLong(java.math.BigDecimal value) {
        return value.longValue(); // truncates
    }

    // Exact conversion (throws if fractional)
    static long convertBigDecimalExact(java.math.BigDecimal value) {
        return value.longValueExact(); // strict
    }

    // Prints all conversion styles for comparison
    static void showConversions(double value) {
        System.out.println("Input decimal (double): " + value);

        long rounded   = convertDoubleToLong(value);
        long truncated = castDoubleToLong(value);

        System.out.println("Rounded (Math.round): " + rounded);
        System.out.println("Truncated (cast to long): " + truncated);
        System.out.println();
    }

    static void showConversions(java.math.BigDecimal value) {
        System.out.println("Input decimal (BigDecimal): " + value);

        long truncated = convertBigDecimalToLong(value);
        System.out.println("Truncated (longValue): " + truncated);

        try {
            long exact = convertBigDecimalExact(value);
            System.out.println("Exact (longValueExact): " + exact);
        } catch (ArithmeticException ex) {
            System.out.println("Exact (longValueExact): ERROR — fractional or out of range");
        }

        System.out.println();
    }

    public static void main(String[] args) {

        // Floating-point examples
        showConversions(12.7);
        showConversions(12.3);
        showConversions(-5.8);
        showConversions(42.0);

        // BigDecimal examples
        // it only succeeds when the BigDecimal represents a whole number that fits inside a long.
        showConversions(new java.math.BigDecimal("12.7"));
        showConversions(new java.math.BigDecimal("42"));
        showConversions(new java.math.BigDecimal("-5.8"));
    }
}



/*
run:

Input decimal (double): 12.7
Rounded (Math.round): 13
Truncated (cast to long): 12

Input decimal (double): 12.3
Rounded (Math.round): 12
Truncated (cast to long): 12

Input decimal (double): -5.8
Rounded (Math.round): -6
Truncated (cast to long): -5

Input decimal (double): 42.0
Rounded (Math.round): 42
Truncated (cast to long): 42

Input decimal (BigDecimal): 12.7
Truncated (longValue): 12
Exact (longValueExact): ERROR ? fractional or out of range

Input decimal (BigDecimal): 42
Truncated (longValue): 42
Exact (longValueExact): 42

Input decimal (BigDecimal): -5.8
Truncated (longValue): -5
Exact (longValueExact): ERROR ? fractional or out of range

*/

 



answered 1 day ago by avibootz
...