0
<div class="form-group">
 <?php
  $data = array('name' => 'Basicvalue','value' => set_value('Basicvalue'),'id'=>'Basicvalue',  'class' => 'form-control' ,'readonly' => 'true');
  echo form_input(($data));
  ?>
</div>  

This is my text box and when I entered an input, it should display with two decimals. How can I do that?

eg: If I entered 11, then the field should display it as 11.00

Ankit Singh
  • 1,477
  • 1
  • 13
  • 22
nidhi
  • 11
  • 2

4 Answers4

1

If you need your action in client side Use following Jquery function :

<div class="form-group">
     <?php
      $data = array('name' => 'Basicvalue','value' => set_value('Basicvalue'),'id'=>'Basicvalue',  'class' => 'form-control changetodecimal' ,'readonly' => 'true');
    echo form_input(($data));
      ?>
  </div>  

<script>
$(document).on('change',".changetodecimal", function(){ 
    var inputvalues = $(this).val();
    var resultvalues=parseFloat(inputvalues).toFixed(2); 
    $(this).val(resultvalues);

});
</script>

If you want Server Side (PHP) use following function :

echo number_format($number, 2);
Karthik
  • 5,589
  • 18
  • 46
  • 78
1

You can use number_format():

return number_format((float)$number, 2, '.', '');

Example:

$foo = "105";
echo number_format((float)$foo, 2, '.', '');  // Outputs -> 105.00
Kamal Chhirang
  • 490
  • 4
  • 14
0

Use number_format($yournymber,2);

<?php
    $num=3;

    $decimalNum=number_format($num,2);

    echo $decimalNum;
?>

You will get the output 3.00

Sunil Rajput
  • 960
  • 9
  • 19
0

In PHP, you need to use number_format() function:

number_format($num,2);
echo number_format(11,2); // 11.00

Numbers are formatted with decimals. In the above example, second parameter defines how many values you want post the decimal.

Milan Chheda
  • 8,159
  • 3
  • 20
  • 35