Link to home
Start Free TrialLog in
Avatar of javadevman
javadevman

asked on

How can I strip decimal points in jsp/java.

I want to trim decimal points from a imported excel spreadsheet using jsp/java.

When I import  numbers such as 30 from a spreadsheet the number displays 30.0
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of javadevman
javadevman

ASKER

Solved my issue
What about trailing decimal numbers. The code above strips all numbers after a decimal point
You can use DecimalFormat class to specify how many decimal digits you need. You can then convert the string to float, then check if after casting to int you'll have the same number and if it is diffrernt then format it with the required numbe fo decimal digits
I think experimenting with DecimalFormat with different constructors made up of zeroes or hash signs in the quoted template will allow you to format your double value in such a way that it prints no dots when the value is equal to integer or will print dots and decimal digits when the value is not exactly equal to integer. Unfortunately for the few days I will not have access to my computer, so I cannot give you the exact way. Using if's to compare if the number is equal to its integer part, though, you can achieve the same result with a little bit more code.
I will experiment with it over the weekend  man. Sorry about your cpu.  Do you know anything about jstl? Any good sites for a newbie?

This is rather old, but pretty logical explanation of the JSTL starting with the explanation of the basics:
http://www.roseindia.net/jstl/introduction.shtml

I guess you needed to use DecimalFormat in this variant:
   DecimalFormat df500 = new DecimalFormat("####.##");


This is how I tested it:

        DecimalFormat df500 = new DecimalFormat("####.##");
        double d30 = 30.0;
        double d3002 = 30.02;

        System.out.println("d30: " + df500.format(d30));

        System.out.println("d3002: " + df500.format(d3002));

Open in new window


This is the output:
d30: 30
d3002: 30.02

Open in new window