NumberFormat problem in java

Hi Experts...
I use a number formatter provide by java to reduce the number of decimal places to two positions after the decimal places.
I have a float variable whose value is 3.44567
and i use number formatter as shown:
Float num=3.44567;

NumberFormat fmt = new DecimalFormat("#0.00");
And convert it by fmt.format(num)

Later when i print the variable it prints 3.44 but when i pass this value to a stored procedure whose type is float and after saving it the number exists in the original format i.e 3.44567.

I dont understand that while printing on the screen it prints properly but when i see the database i get all the numbers even after two decimal places.
Can anyone guide how to rectify this.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

fmt.format(num) produecs a String value just for the priniting purposes

It does not affect the number num itself
format method  takes the double or float number amnd returns String
the number itself is not affected by the method
gaugetaAuthor Commented:
@for_yan:Thanks for the reply.
How can i really achieve this behaviour.
Please help...
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

when you then pass num to stored procesdure - you pass original number which is reorenstented the way double nujhmedrs are re;resented in memory
I think - this is your oprtimal behavior - you want to stire double numbers with all their digots and manipulate them with all theor didgint and only then when you need to print in the last momemt then you wnat to format
If you want you of course can do the following:

float f = (new Float(fmt.format(num))).floatValue();

and then you'll loose your digits after the second - but this is not recommended by any means
gaugetaAuthor Commented:
All i need to do is to make sure that the numbers stored in the database do not have more than two numbers after the decimal point.
How can i achieve the same.
Sometimes you can use database options to round flost numebsr when you slect them for printing.
Oracle jhhas simple finction

select round(my_num, 2) from table

but do it only if you sleect for printing
All calculations should be done on the full numbers and only then they should be formatted otherwise you may loose accuracy
You can do it this way:

 num = (new Float(fmt.format(num))).floatValue();
and then store num
>All i need to do is to make sure that the numbers stored in the database do not have more than two numbers after the decimal point.

keep in mind that that is not very sensibel requirement - database will store numbers still the way it wants to store floating numbers. so if you really want to store float numbers you rather store tham as they are with all didgits and the use rounding or dformatting when when you slect them
If you don't carea bout their numeric properties (don't want to add or subtract them aor calculete averages) then you format them - make strings of them and store them as text field in the database - then you are guranteed that you'll select them with all exactly same difists as you stored them
Otherwise you cannot control their internal representations in the databse and there is no guratntee after you slecte them back that even if you stored numbe
 f= 5.02;

there is no guarantee that on slecet you'll not get


If you use round(col_name,2) in Orcale then you would be guarnateed to have 5.02, but for that you need not store it necesarriry as exactly 5.02 - your rounding will anyway happen on the select stage
In fact it is betetr not to fight thsi system and try to store in database two digits excatly,  but rather take advantage of this syaterem - and do all storage and calculations with float numbers as float and only on the priontout or display do actual formatting
Let me know if you have any further questions, this is something which is really important to understand
gaugetaAuthor Commented:
@for_yan:Thanks for the help.
I tried creating a new float variale and assigning the value from the formatted number using the following syntax: (new Float(fmt.format(f55UpRating))).floatValue()) .
I sent this value directly to db but still its having all the numbers after the decimal places.
Please help...
You need to assign.
nothing happns with f55Uprating number here:
(new Float(fmt.format(f55UpRating))).floatValue()) .
All i need to do is to make sure that the numbers stored in the database do not have more than two numbers after the decimal point.

Why do you want to do that? It will simply make any calculations you do with them less precise
You need to assign:

f55UpRating = (new Float(fmt.format(f55UpRating))).floatValue()) ;


then send
to database
As I said - it all does not gurantee you taht wnhe you select you'll not get

even though you stored 5.02

That's why as I many times explaine - store as they are - only on printout and display format -just before dosplay, not before storage
Thisis compiled and tested

           float  f55UpRating =  3.44567f;
        NumberFormat fmt = new DecimalFormat("#0.00");
        f55UpRating = (new Float(fmt.format(f55UpRating))).floatValue() ;


Open in new window



Open in new window

gaugetaAuthor Commented:
@for_yan:Even i get the same thing.
I tried assigning it to a variable and then sending to the db,but still has decimal numbers aftertwo decimal places.
I undersatnd that there is no guarantee that the same value will return but since this is a reqirement this needs to be done.
Is there any other way to do the same.
You'd need to use a different column type - string or int
As I wrote you before use string or varchar2 ior what ever it is in the database type that you are using
and store

String s = fmt.format(f55UpRating);

now store s in database

pst.setString(1, s);
gaugetaAuthor Commented:
@for_yan:I tried to insert usuing the fowllowing syntax:

String s = fmt.format(p2UpRating);
pst.setString(1, s);

com.sybase.jdbc2.jdbc.SybBatchUpdateException: JZ0BE: BatchUpdateException: Error occurred while executing batch statement: Implicit conversion from datatype 'CHAR' to 'FLOAT' is not allowed.  Use the CONVERT function to run this query.
The datatype  of the databse is Float.
How to retify this error?
in the colmun which is supposed to be float you acnnot store text/string - you need to add oine more column to the table which would contain stringh and you can store thete the number as string
The problem is that the requirement to have  a number in the database number column with two decimal digits cannot have any sense. Number of decimal digits odf the number when it is stored in the float column of database or just in memeory of the computer is not defined; it is only when you print or display the number in certain fromat that you can talk about the number of decimal digits.
In database you can talke about the accuracy of that number - say up to second decimal digits - but this by no means would mean that it has two decimal digits in its representation - this formula woul mean that the value you obtained is no more than by 0.01 differs from the actual value. Idf that was the requirement - it does not mean that it will have tow decimal digits. Number fo decimal digits of the numeber in its internal representation is not defined; only its accuracy may be defined.

And you of course would not be able to store characters in the filed which is supposed to be the number, that is exactly because numebrs are stored and represented in quite different ways, not as strings representing numbers. To store in that way tyou need to define special column which will be a test/character column,  not the number

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.