Design of price type in the database of small e-commerce system

Source: Internet
Author: User

Today share the data type design of a database Price field for a small e-commerce system. Enclose the general rounding conversion method

We know that the type of price field used, the best of the two are: Decimal,money, and the money of the decimal point can only be accurate to 4 bits, although money in memory is less than a decimal byte, but now the hard disk so big, do not care.

Personally like, I all directly with decimal (18,5), small number Part I directly use 5 bit;

However, for a product, I will only use a maximum of two decimal places, the percentage will only use 4 bits, 5-bit can only be a smaller commission ratio calculation.

But I think the number is really too small, since for small e-commerce, I think as long as two is enough,

So I introduced some concepts,

1, for the withdrawal amount of the fee does not use the standard rounding, retain two decimal places.

2. The Commission fee is not rounded by the standard, and two decimal places are reserved.

3, to the user to pay the rounding, as long as the third decimal value, directly to the second 1, the purpose is to users pay more money.

3, to businessmen to pay the rounding, regardless of whether the third decimal value, are not in 1, the purpose is to business less pay.

Well, with these basics, I direct the entire database design saved price Value Commission values are retained two decimal places to save, although using decimal (18,5) will have 3 extra 0, here I directly with a method to cut, anyway is not worth.

And for the percentage, the direct unchanged, are the use of five decimal places.

Below I offer my method of conversion:

    /// <summary>    ///Rounding Calculation Classes/// </summary>     Public classRound {/// <summary>        ///standard Cut, results reserved two decimal places///do not calculate rounding/// </summary>         Public Static decimalStandard (stringMoney ) {            return decimal. Parse ((Math.truncate (Double. Parse (Money) * -) /100.00). ToString ("0.00")); }        /// <summary>        ///Standard Rounding, results reserved two decimal places/// </summary>         Public Static decimalRoundforstandard (stringMoney ) {            returnStandard (Math.Round (Double. Parse (Money),2, Midpointrounding.awayfromzero).        ToString ()); }        /// <summary>        ///rounding for the user, result reserved two decimal places///to the user to pay the rounding, the purpose is to users pay more money/// </summary>         Public Static decimalRoundforuser (stringMoney ) {            if((Double. Parse (Money) * -) > ((int) (Double. Parse (Money) * -)))//See if there's a number in the third decimal place .            {                //in some cases, go straight into 1.                returnStandard ((Double. Parse (Money) +0.01).            ToString ()); }            Else            {                returnStandard (money); }        }        /// <summary>        ///rounding for merchant, result reserved two decimal places///to the merchant to pay the rounding, the purpose is to business less pay/// </summary>         Public Static decimalRoundformerchant (stringMoney ) {            returnStandard (money); }        /// <summary>        ///fixed point conversion, can be removed after the extra 0 decimal///This is not fixed how many decimal places are reserved/// </summary>         Public Static decimalFixed (stringMoney ) {            return decimal. Parse (string. Format ("{0:g}", Money)); }    }

Something, if there is anything wrong with me here, please correct me.

Design of price type in the database of small e-commerce system

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.