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.
Related
My table field is date/time and formatted like this:
mm/dd/yyyy hh:nn:ss
I want the user to see this (with the space appearing between date and time
__/__/__ __:__:__
I want an input mask that demands:
Either 1 or 2 digits for the month
Either 1 or 2 digits for the dat
All 4 digits for the year
SHOWS the space but just jumps over it for the user
Either 1 or 2 digits for each of Hours, Minutes and Seconds
Further, when setting up a DB, is it just smarter to have two separate fields for Date and Time. A collegue encouraged me to break them out ... seems sensible?
00/00/0000##00:00:00
See the outcome in the image
Controlled user input is not an easy task in Access, as it is optimised for the opposite: To be tolerant and accept many input sequences for date and time.
For the cases where controlled input is mandatory, I've written two articles including full code (too much to post here) and demo, that may give you some ideas:
Entering ISO formatted date with input mask and full validation in Microsoft Access
Current code at VBA.DateEntry.
and
Entering 24-hour time with input mask and full validation in Microsoft Access.
Current code at VBA.TimeEntry.
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.
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.
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
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.