0

I need to insert a dynamic decimal in mysql. Meaning it could be 7.5 or 33.5 or 150.5. The final value is a percentage based on sales. I found this answer on stack: How to store decimal in MySQL? but it does not help me with my question.

Is there a simple way to store these examples?

3.5 or 47.5 or 246.5

If I use varchar the decimal does not show up. If I use the code form the answer above the decimal is off. Any suggestions?

UPDATE: Actually I think its my code. I can't seem to get the decimal to be in the right place. I am trying to get 30% of a random number. This code is not working right below

<?
$randomMade = 40;
$TotalMade = (30/$randomMade) * 100;
$TotalMade = round($TotalMade);
echo $TotalMade; // Comes out as 75 - But it should be 7.5 

?>

UPDATE: Found percentage issue:

$TotalMade = (30*$randomMade) / 100; I had the math wrong. :(
Community
  • 1
  • 1
Papa De Beau
  • 3,744
  • 18
  • 79
  • 137

1 Answers1

0

Use decimal(5,2) if you want to store up to three digits to the left of the decimal.

You can adjust the "5" for larger values.

But, you can just store it as a floating point number, and then use format() to create a string:

select format(123.5, 1)

Produces . . .

 123.5

EDIT:

In php, try:

$TotalMade = round($TotalMade, 1);

The second argument is the precision.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786