-2

How to display decimal for input text taken from a database, onload in JavaScript.

Am having value as 100 when displaying it from the database into a text box. It should be 100.00. Is it possible.

Hidde
  • 11,493
  • 8
  • 43
  • 68
user2419839
  • 63
  • 1
  • 3
  • 9
  • possible duplicate of [JavaScript: formatting number with exactly two decimals](http://stackoverflow.com/questions/1726630/javascript-formatting-number-with-exactly-two-decimals) – CRABOLO Mar 01 '14 at 11:51
  • but am getting the value from session so that only it is not known how to specify value="session->store['actual_spending']['actual_total_private_funding']) ? '' : $this->session->store['actual_spending']['actual_total_private_funding']; – user2419839 Mar 01 '14 at 11:55
  • why do you use javascript for that...if your values come from db then you can display using php – Prafulla Mar 01 '14 at 11:58
  • how to give it in the above code? – user2419839 Mar 01 '14 at 11:59

2 Answers2

3

Javascript

function onclick(id)
{
  document.getElementById('text').value = id.toFixed(2);
}

you can set 100 as 100.00 using php

<input type="text" value="<?php echo number_format($row['numbert'],'2','.',''); ?>" />
Prafulla
  • 600
  • 7
  • 18
  • hi prafulla am getting into one variable $('#actual_total_public_funding').val(addCommas(item.total_public_funding)); here am adding commas can i add decimal here along with comma? – user2419839 Mar 01 '14 at 12:16
  • @user2419839 I gt your question about comma and decimal and answer is [here](http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript) – Prafulla Mar 01 '14 at 12:30
0

You can use to toFixed javascript function:

var x = 100;
document.getElementById('textbox').value = x.toFixed(2);
Ashok Shah
  • 956
  • 12
  • 39