This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Numeric Format in Label with Thousand comma and two decimel  RSS feed

 
Ranch Hand
Posts: 39
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone

Need help on how to show the Numeric Format in the Label and TableView Column.

Below is my Code;

Tried may approaches and i failed;





Which is then Loaded to Below;



Kindly Help

Thanks
 
Rancher
Posts: 1170
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What isn't working? Any errormessages? I can tell you this in advance: That's not how you create a prepared statement...
Have a look at how it's done
 
Ifraz Imanudeen
Ranch Hand
Posts: 39
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:What isn't working? Anny errormessages? I can tell you this in advance: That's not how you create a prepared statement...
Have a look at how it's done



Well all YouTube videos i saw shows this kind of preparedstatement...

I will go by oracle guidelines

Error Messages nothing show.
All relevent data is show in Textfields and tableview.

Its just that the Value shown in label is lie 2330.96 whereas i need to show it as 2,330.96 with thousands comma.
 
Daniel Demesmaecker
Rancher
Posts: 1170
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your data is shown the statement isnt the prob. For the formatting this should do the trick:



 
Ifraz Imanudeen
Ranch Hand
Posts: 39
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:If your data is shown the statement isnt the prob. For the formatting this should do the trick:







Thats giving an ERROR


Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 56 more
Caused by: java.util.UnknownFormatConversionException: Conversion = ','
at java.util.Formatter.checkText(Formatter.java:2579)
at java.util.Formatter.parse(Formatter.java:2565)
at java.util.Formatter.format(Formatter.java:2501)
at java.util.Formatter.format(Formatter.java:2455)
at java.lang.String.format(String.java:2940)
at loginfx.InvoiceContoller.GrandSum(InvoiceContoller.java:193)
at loginfx.InvoiceContoller.getBillNo(InvoiceContoller.java:86)
at loginfx.NewOrderController.ShowBillInvoice(NewOrderController.java:602)
... 66 more

Numeric.JPG
[Thumbnail for Numeric.JPG]
GrandTotal Remains Same
 
Daniel Demesmaecker
Rancher
Posts: 1170
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use a pattern to format your number.

De poundsigns represent a nr. De comma is your seperator and the point your decimal sign. SO now you have every three nrs a seperator and two decimal points (there only 2 pound signs at the end)
Note : that you will have to parse your String to a long before formatting. Dont forget the L on the end
Sorry if the explenation isn't that clear, but I don't really know how to explain in english
 
Ifraz Imanudeen
Ranch Hand
Posts: 39
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:You could use a pattern to format your number.

De poundsigns represent a nr. De comma is your seperator and the point your decimal sign. SO now you have every three nrs a seperator and two decimal points (there only 2 pound signs at the end)
Note : that you will have to parse your String to a long before formatting. Dont forget the L on the end
Sorry if the explenation isn't that clear, but I don't really know how to explain in english



Thanks for your support,
but still things are not working,



ERROR

Caused by: java.lang.NumberFormatException: For input string: "2335.0"
at java.lang.NumberFormatException: For input string(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:589)
at java.lang.Long.parseLong(Long.java:631)
 
Marshal
Posts: 64496
225
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ifraz Imanudeen wrote:. . .. . .

Please always copy'n'paste your code; you have introduced two compiler errors into line 2, but I am 99% sure they weren't in the original.
I can see other errors in that code: why are you storing numbers as Strings in your database? Surely you would store money as a DECIMAL(8, 2) or similar; that will allow any amount < £$₹€₽¥1000000, I think. Then you get the result from the result set as a BigDecimal (I think).
Then I am not sure whether you are using formatters or decimal format. When Formatter was introduced in 2004, I was told that it is no longer necessary to use decimal format and similar, and I have believed that. So what I would do is thisThe comma means to use the thousands separator appropriate to that Locale, and .2f means to format it as a fixed‑point (decimal) number with two digits after the decimal point. If you look up String#format(), you will find that the Locale is optional because the method is overloaded.

[edit]Correct spelling errors and add “String.format(...)”.
 
Daniel Demesmaecker
Rancher
Posts: 1170
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I pointed out that you have a typo on line 2 and sugested how to parse properly or use valueOf, but since I afterwards saw C.R.'s answer I removed it
 
Master Rancher
Posts: 4087
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Ignoring the typos for the moment, this is assuming you are getting a long value from the 1st column in your ResultSet.

The error, however, shows this:
Caused by: java.lang.NumberFormatException: For input string: "2335.0"
at java.lang.NumberFormatException: For input string(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:589)
at java.lang.Long.parseLong(Long.java:631)

This has nothing to do with the DecimalFormat, as your code doesn't even get to the line where that is used.

It is failing because the value in the column is 2335.0, which is not a valid long.

And Campbell is quite correct, why on earth is your database storing numbers as a String?
 
Ifraz Imanudeen
Ranch Hand
Posts: 39
Java MySQL Database Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Ifraz Imanudeen wrote:. . .. . .

Please always copy'n'paste your code; you have introduced two compiler errors into line 2, but I am 99% sure they weren't in the original.
I can see other errors in that code: why are you storing numbers as Strings in your database? Surely you would store money as a DECIMAL(8, 2) or similar; that will allow any amount < £$₹€₽¥1000000, I think. Then you get the result from the result set as a BigDecimal (I think).
Then I am not sure whether you are using formatters or decimal format. When Formatter was introduced in 2004, I was told that it is no longer necessary to use decimal format and similar, and I have believed that. So what I would do is thisThe comma means to use the thousands separator appropriate to that Locale, and .2f means to format it as a fixed‑point (decimal) number with two digits after the decimal point. If you look up String#format(), you will find that the Locale is optional because the method is overloaded.

[edit]Correct spelling errors and add “String.format(...)”.



Thanks you Very Much Daniel Demesmaecker and Campbell Ritchie

Thanks for your Time and Support,

Got it working



and the output was 2,335.00

The Database i am using is SQLite

Thanks again Daniel Demesmaecker and Campbell Ritchie

Great Help
 
Daniel Demesmaecker
Rancher
Posts: 1170
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:

Ignoring the typos for the moment, this is assuming you are getting a long value from the 1st column in your ResultSet.
...

It is failing because the value in the column is 2335.0, which is not a valid long.


Why do you think he's suppose to get a long? He's getting a String and trying to parse it to a long.

And Campbell is quite correct, why on earth is your database storing numbers as a String?


I also often store a number as a String, as long you don't have to do calculations with it, what does it mather?
 
Dave Tolls
Master Rancher
Posts: 4087
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:
Why do you think he's suppose to get a long? He's getting a String and trying to parse it to a long.



It's because he's parsing it as a long that means he needs a long to start with.
That's the error.
That String does not contain a long...it's a float of some description.

Daniel Demesmaecker wrote:
I also often store a number as a String, as long you don't have to do calculations with it, what does it mather?



It's a number, so it should be treated as such, otherwise you get these problems.
There should be no need to parse a number retrieved from the database.

It's the same problem as storing DATEs as VARCHAR, or phone numbers as NUMBER when they're VARCHARs.
Store the data as the type it represents then you don't have to jump through hoops later on to actually use it.

In addition, you also get the correct ordering for the column if it's a NUMBER.
 
Daniel Demesmaecker
Rancher
Posts: 1170
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:
It's because he's parsing it as a long that means he needs a long to start with.
That's the error.
That String does not contain a long...it's a float of some description.


He dosn't start with a long, he start with a string, it should just be parseable to a long, but it always be a String

Dave Tolls wrote:
It's a number, so it should be treated as such, otherwise you get these problems.
There should be no need to parse a number retrieved from the database.
It's the same problem as storing DATEs as VARCHAR, or phone numbers as NUMBER when they're VARCHARs.
Store the data as the type it represents then you don't have to jump through hoops later on to actually use it.
In addition, you also get the correct ordering for the column if it's a NUMBER.


All these examples you've given you should be able to do some calculations on it.
If you don't need to do calculations on it, there isn't a need to parse it eather.
I wasn't saying he should store at as a string in this case, but a housenr in an address for example, what does it mather if it's a number or a String
A date I wouldn't store as varchar eather, but once again, you should be able to do calculation on a date (add days or whatever)
Like a said before if you don't have to do calculations on it, it dosn't mather
 
Dave Tolls
Master Rancher
Posts: 4087
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:
He dosn't start with a long, he start with a string, it should just be parseable to a long, but it always be a String



But the String is a double, the value was (as the error shows) 2335.0, which the parse method cannot parse as a long, due to the decimal point.
That's why I am saying that the value he is working with is not a long, anymore than 2335.1 would be a long.

Daniel Demesmaecker wrote:
I wasn't saying he should store at as a string in this case, but a housenr in an address for example, what does it mather if it's a number or a String



A house number is a String because 5A is a valid house (or flat) number.
The same way a phone number is a String.

ETA: I know what you mean (hence my mentioning phone numbers), but in this case this is clearly a number...even the OP knows that as he is turning it into a long.
 
Ifraz Imanudeen
Ranch Hand
Posts: 39
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:

Ignoring the typos for the moment, this is assuming you are getting a long value from the 1st column in your ResultSet.

The error, however, shows this:
Caused by: java.lang.NumberFormatException: For input string: "2335.0"
at java.lang.NumberFormatException: For input string(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:589)
at java.lang.Long.parseLong(Long.java:631)

This has nothing to do with the DecimalFormat, as your code doesn't even get to the line where that is used.

It is failing because the value in the column is 2335.0, which is not a valid long.

And Campbell is quite correct, why on earth is your database storing numbers as a String?



Thanks for the explanation
The database i am using is SQLite
Which has very few data types. I have select Numeric for the column Price

👍
 
Campbell Ritchie
Marshal
Posts: 64496
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:. . . as long you don't have to do calculations with it, what does it mather?

If you don't do calculations, then it isn't a number. A house number, a national insurance/social security number, or a phone number are not numbers in that sense, as Dave has already said; you don't do any arithmetic with them. But storing genuine numbers as Strings is often wasteful in memory because Strings occupy more space, and error‑prone because you can't be sure that you have stored the correct format for a number. Also, you have the overhead of encoding the number as a String and decoding it later with the parseXXX method.

Dave Tolls wrote:. . . But the String is a double . . .

[pedantic mode]You mean the String is the text representation of a double, as opposed to the text representation of a long.[/pedantic mode]
 
Dave Tolls
Master Rancher
Posts: 4087
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:[pedantic mode]You mean the String is the text representation of a double, as opposed to the text representation of a long.[/pedantic mode]



It's a fair cop.

 
Campbell Ritchie
Marshal
Posts: 64496
225
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ifraz Imanudeen wrote:. . . SQLite
Which has very few data types. . . .

Maybe you should consider a different database. The only datatype it has suitable for money appears to be INTEGER.
 
Ifraz Imanudeen
Ranch Hand
Posts: 39
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Ifraz Imanudeen wrote:. . . SQLite
Which has very few data types. . . .

Maybe you should consider a different database. The only datatype it has suitable for money appears to be INTEGER.



My client needs a portable application, thats why i choosed SQLite. Or else i started project with MySql.

Thats the reason...

Anyway i have learnt a lot from this post n above experts

Thanks alot.
 
Ifraz Imanudeen
Ranch Hand
Posts: 39
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Ifraz Imanudeen wrote:. . . SQLite
Which has very few data types. . . .

Maybe you should consider a different database. The only datatype it has suitable for money appears to be INTEGER.



Did you say integer

SQlite has datatype Int. Should change from Numeric to Integer and see, how things don't get worse.
 
Dave Tolls
Master Rancher
Posts: 4087
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But remember, as an Int the data will now be in pennies (or cents, or whatever the smaller unit is) and not pounds (or euros, or dollars).
 
Campbell Ritchie
Marshal
Posts: 64496
225
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ifraz Imanudeen wrote:. . . Did you say integer . . .

No, I said INTEGER, which is what the link I posted earlier said. As Dave has told you, you will be storing the data in cents/pence etc and will need % 100 and / 100 to convert it. Maybe there are different names for integers, like INT, LONG_INT etc.
 
Ifraz Imanudeen
Ranch Hand
Posts: 39
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Ifraz Imanudeen wrote:. . . Did you say integer . . .

No, I said INTEGER, which is what the link I posted earlier said. As Dave has told you, you will be storing the data in cents/pence etc and will need % 100 and / 100 to convert it. Maybe there are different names for integers, like INT, LONG_INT etc.



I see.
Present situ is much better. 😁
 
Ifraz Imanudeen
Ranch Hand
Posts: 39
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:But remember, as an Int the data will now be in pennies (or cents, or whatever the smaller unit is) and not pounds (or euros, or dollars).



Thanks will keep that in mind 👍
 
Bras cause cancer. And tiny ads:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!