Errol Chaves Moya
Errol Chaves Moya

Reputation: 377

HTML input text: hide charaters without removing them

I hope you can help me: I have an HTML-PHP software that makes the calculations with 5 decimals extrictly. The point is, I need it to be 5 decimals always for the calculations; but I want to show only 2 decimals to the user without deleting the other 3 decimals (the 5 decimals have to be in the page always but only the first two decimals visible).Is there a way to do it? I'll add a picture of what it looks like right now:

example

Upvotes: 0

Views: 51

Answers (3)

anakin
anakin

Reputation: 35

You could do the solution presented before me to show the numbers on the text box

<input type="text" id="shownumber01" name="shownumber01" value="<?php echo number_format((float)$number, 2, '.', ''); ?>">

Additionally in order to pass the numbers with with all the decimals points you want through ajax later in the order you could use hidden values

 <input type="hidden" id="fullnumber01" name="fullnumber01" value="<?php echo number_format((float)$number, 6, '.', ''); ?>">

The textboxes seem to be readonly anyways, but if not you could look into a number mask with javascript, there are many choices online on a quick google search.

Santiago G.

Upvotes: 1

Rafael Santos
Rafael Santos

Reputation: 313

it would be nice to have some of the code to formulate a proper answer, but just to think outside the box here:

if I understood correctly the problem is that you want the user to see a 2 decimals version of a variable, but you still wanna keep the original 5 decimals version of that variable and perform calculation on that right?

How about using 2 variables, one for display, and the main one for calculation. They behave like mirroring each other, every time the main variable changes, you also change the display variable

Upvotes: 1

Mazsi
Mazsi

Reputation: 76

Use number_format() function when echo-ing the data like

<?php echo number_format((float)$number, 2, '.', ''); ?>

Upvotes: 2

Related Questions