TextBox numbers formatting & data binding

Hi, I’m looking through the docs and forum but cannot find the answer - is it possible to elegantly define a number format for a TextBox of a number type which is bind both ways to a data table? My app deals with a rather huge numbers, like 63117869662.0971 for which I’d like to display 63,117,869,662.0971.

One way to deal with this is through show and change events, probably?

Thanks, Tomaz

My experience is that a number field does not accept punctuation (currency signs and commas).

  • If typed, the keypress is ignored.
  • If pasted, then on loss of focus, the field is blanked.

This appears to be standard for HTML number fields. To contain any kind of formatting, the field would have to be of type “text”.

I don’t know whether that would work for a column of type “number”.

1 Like

I use a Link (formatted such as: "{0:.2f}".format(number) ) which hides on click and shows a number TextBox instead. Then on lost_focus of the textbox the textbox is hidden and Link shown with the formatted number.
tb_link_format

8 Likes

Hi @stu, it’s awesome. Thanks

Great, thanks! Coupled with refresh_data_bindings() it works really nice.

Hi there! I’m new to anvil and even though the answer reads simple to implement I’ven’t been able to replicate it.
I suppose that you defined a click event (def link_1_click(self, **event_args): ) and then do the steps you mention after. How exactly is that done? I’ve been trough the anvil documentation and didn’t get any clear idea on how to link to a text box neither to the lost_focus or link formatting.
I’d really appreciate any help to understand this example.
And other question I was wondering, the number written can be then called from a function as a usal text box?

Welcome to the forum!

This is an old question that has already been answered, you are not showing what you have tried and are not formatting the code in the post. You are unlikely to get an answer here.

If you want to increase your chances, go through some tutorials and when you have something to show that doesn’t work, create a new question: How to ask a good question

1 Like

Hi Stu,

I am having trouble understanding what exactly you are doing here. Would you be able to show me an example app please?

Regards
Steve

Hello @steve.page and @new_pystronaut_27!

Here’s a demo of Stu’s technique:

https://forum-demo-textbox-number-formatting.anvil.app

And here’s a link to clone it: Anvil | Login

1 Like

Thanks @tomherden that looks very much like what I am trying to achieve.

How do I find out what he is doing behind the scenes?

Regards
Steve

I’ve added a clone link to my previous post – I apologize for having overlooked that step at the time! :laughing:

2 Likes

@tomherden Tom you are a superstar - no need for an apology. It would have taken me days to work out how to do that.

Many thanks
Steve