How to prevent to store also the 0 in decimal? - mysql

I need to save the avg of a value, that in this case corresponds to the average attendance of a venue, and for this, I created a new field called avg which has this structure: decimal(10,3).
Now if I store a value like this: 2671, the field will contain: 2.671 that's correct, but if I need to store only 800 I'll get: 800.000 that's, of course, is a different number…
So how can I store the correct decimal value? In this case should be only 800, not 800.000.
Also, how can I adjust the bad values stored in the database without repeat all the insert?
Thanks.

The decimal point is irrelevant, you can trim the decimal point using another function.

Related

Laravel Integer to Float or Decimal MySQL Column

I have a current db table called scores that has 2 columns user_score and approved_score which are both Integer.
After initial launch we have decided to make things more exact by add a decimal to a score. So instead of getting a 10 you could get a 10.40 or a 10.47 ect. I am just curious on:
Should I change it to a decimal or a float based on us needing 2
decimal points with trailing 0. So: $table-> decimal('user_score', 8,
2)->change();
Will that mess up data in there already? Like will a
score of 123 stay 123? We rank based on score so the higher the
better.
A float would be more appropriate in this case, it doesn't sound like you need a high number of precision points after the decimal place. The data should not be impacted by changing the column type, so long as your values are not currently larger than (8,2).
Also, updating your existing data if you kept the column as an integer would be trivial as all you would need to do is UPDATE column SET column=column*100.

Mysql datatype for money

i was trying to create a money related app in which users can choose their currency. Mysql datatype i tried is decimal(19,4). Now the problem is few currencies need three precisions and some need two
Eg:
oman rial needs three precisions. ie 1000 baisa = 1 omani rial. Hence my customers may enter 6.783 omani rial.
Where as my US customers will need only 2 precisions as 100 cents = 1 dollar and they may enter 5.50.
When i insert these two entries to my database using decimal(19,4), it is saved as 6.7830 and 5.5000 respectively.
Now the real pain is when i need to display their entrys as i dont want to display that extra 0 in omani rial entry and that 00 in US dollar. I also tried float but last digit gets rounded off at times.
Is there any mysql data type in which i can save exact entry as it is without any rounding off or extra zeros? If there is no such entry, how can i make it ppssible?
You can use VARCHAR to store exact representations, but I don't recommend that because it takes more bytes to store a number as a string. And any arithmetic you do on the value will convert it to a number anyway.
I recommend you use DECIMAL(19,4), and then format the value in application code, to display it with the appropriate digits. Every programming language has some function like printf() that allows you to control the output formatting, regardless of the value stored.

JPA / MySQL Decimal displayed in wrong way

I want to persist a sum of double values as Decimal(4,2) in my database and display it to the user. The issue I have is, that the decimal number is stored correctly in my database with two decimal digits. But when querying it from the database in some cases it displays a lot more decimal digits to the user.
In my Entity class I have:
#Column(name="weight_sum", precision=6, scale=2)
private double weightSum;
In my MySQL Database I have a column specified as
Decimal (4,2)
I don't understand where I made the mistake.
Actually you are doing correct things but you need to do some more stuff.
If you have define precision and scale for weightSum of entity class then it will save into db accordingly.
But when you get back data from db and bind to corresponding entity class then it will again take default scale(actual scale following by zero) and precision value.
So whenever you send data to end user you need to set scale as-
weightSum.setScale(2, BigDecimal.ROUND_FLOOR);
In above method 2nd parameter is roundingMode, you should select roundingMode as per your requirement.
I hope it will help you out. Thanks

What datatype should I use for my Tickets table?

I'm creating a table which is set to list out the prices of tickets for specific venues of a tour in my database.
The price needs to be in UK currency (£0.00) as the tours are based in the UK. However when it comes to setting the datatypes for the price columns I was unsure what datatype would allow the format of UK currency to fit. So I used DOUBLE PRECISION but when trying to input these values:
INSERT INTO `tbl_Tickets` (`Student_Price`, `Adult_Price`, `OAP_Price`, `Tourdate_id`)
VALUES ('£15.00', '£22.00' ,'£13.50', 1);
I get a 0, each of the price columns.
What datatype should I set my price columns to, to allow UK currency to properly fit?
You should probably use a fixed-point numeric type to store currency of any nationality, eg. DECIMAL(5, 2) which would store values up to 999.99.
As spencer7593 points out below, there's no need to be stingy with the number of digits, as the DECIMAL type's storage format is quite compact.
You must never, never, never use a floating-point datatype for money. You must always use a decimal radix, in this case DECIMAL(N, 2), where N is some reasonably high upper bound on values, their sums, etc, such as ten, or much more if you're running a bank. The 2 is because you have 100 pence to the pound, but again there are cases where you might want a bit more.

Having trouble converting my decimal data type to just 2 decimal places

Need some help here. I'm trying to update a column in one of my tables to take the Spend amount from 1 column and divide it by 0.85 and display at with only 2 decimal places. I have tried the ROUND function and I am currently using the CAST function to list the field as a DECIMAL in which I've tried (38,2), (30,2), etc. and none work. Here's a copy of what I'm trying to do :
(CAST(NetMediaSpend as decimal (38,2)))/.85
and even after I run it and look at my data I still see the value stored as :
13712.941176
How do I get it to only display 2 decimals of 13712.94 ?
CAST(NetMediaSpend / .85 AS DECIMAL(38,2))
select format(NetMediaSpend/.85,2) from your_Table
I was finally able to figure out how I needed to write it:
ROUND(CAST(NetMediaSpend as DECIMAL(30,2))/.85,2)