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.
gaugetaAsked:
Who is Participating?
 
for_yanConnect With a Mentor Commented:
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
0
 
for_yanCommented:
fmt.format(num) produecs a String value just for the priniting purposes

It does not affect the number num itself
0
 
for_yanCommented:
format method  takes the double or float number amnd returns String
the number itself is not affected by the method

http://docs.oracle.com/javase/1.4.2/docs/api/java/text/NumberFormat.html#format%28double%29
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
gaugetaAuthor Commented:
@for_yan:Thanks for the reply.
How can i really achieve this behaviour.
Please help...
0
 
for_yanCommented:
when you then pass num to stored procesdure - you pass original number which is reorenstented the way double nujhmedrs are re;resented in memory
0
 
for_yanCommented:
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
0
 
for_yanCommented:
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
0
 
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.
0
 
for_yanCommented:
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
0
 
for_yanCommented:
You can do it this way:


 num = (new Float(fmt.format(num))).floatValue();
0
 
for_yanCommented:
and then store num
0
 
for_yanCommented:
>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
0
 
for_yanCommented:
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
0
 
for_yanCommented:
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;
pst.setFloat(1,f);

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

5.0199999999;

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
0
 
for_yanCommented:
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
0
 
for_yanCommented:
Let me know if you have any further questions, this is something which is really important to understand
0
 
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...
0
 
for_yanCommented:
You need to assign.
nothing happns with f55Uprating number here:
(new Float(fmt.format(f55UpRating))).floatValue()) .
0
 
CEHJCommented:
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
0
 
for_yanCommented:
You need to assign:

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

System.out.println(f55UpRating);

then send
f55UpRating
to database
0
 
for_yanCommented:
As I said - it all does not gurantee you taht wnhe you select you'll not get
like
5.019999999

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
0
 
for_yanCommented:
Thisis compiled and tested

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

        System.out.println(f55UpRating);

Open in new window


Output:

3.45

Open in new window

0
 
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.
0
 
CEHJCommented:
You'd need to use a different column type - string or int
0
 
for_yanCommented:
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);
0
 
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?
0
 
for_yanCommented:
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
0
 
for_yanCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.