Results 1 to 4 of 4

Thread: Data types

  1. #1
    Join Date
    Apr 2003
    Posts
    4

    Question Data types

    Yo guys, me again, in mysql is there a data type one can use specificaly for 'money' or do I have to use the 'decimal' data type?

    Cheers - Falc >;-)

  2. #2
    Join Date
    Feb 2003
    Location
    Johannesburg, South Africa
    Posts
    145
    I suppose many will not agree totally with my method, but I am happy with it. What I do is to simply store the value as an Integer - which means that $3.55 will be 355 in the table.

    The application side then deals with all the rest.

    The only reason is that you can manipulate Integers easier then other data types, and is easier portable accross many programming languages. I use Perl most of the time, and I have a simple routine to convert the integer into it's relevant currency:

    Code:
    sub cconvert {
    
    	# use: cconvert( 'integer', 'currency-symbol' );
    	#      integer         => integer value of the currency in cents
    	#      currency-symbol => the string representation of the currency
    
    	if ( $_[0] && $_[1] ) {
    	
    		my $tval = $_[1] . ( $_[0] / 100 );
    		return $tval;
    	
    	} else {
    	
    		# error
    		return -9999;
    	
    	}
    
    }
    And here is a sample use:

    Code:
         1	#!/usr/bin/perl
         2	
         3	$value = cconvert( '655', '$' );
         4	if ( $value =~ /^-9999$/ ) { 
         5	
         6		# an error has occured - deal with it
         7	
         8	} else {
         9	
        10		print "$value\n"; 
        11		
        12	}
        13	
        14	$value = cconvert( '777' );
        15	if ( $value =~ /^-9999$/ ) { 
        16	
        17		# an error has occured - deal with it
        18	
        19	} else {
        20	
        21		print "$value\n"; 
        22		
        23	}
        24	
        25	exit;
        26	
        27	sub cconvert {
        28	
        29		# use: cconvert( 'integer', 'currency-symbol' );
        30		#      integer         => integer value of the currency in cents
        31		#      currency-symbol => the string representation of the currency
        32	
        33		if ( $_[0] && $_[1] ) {
        34		
        35			my $tval = $_[1] . ( $_[0] / 100 );
        36			return $tval;
        37		
        38		} else {
        39		
        40			# error
        41			return -9999;
        42		
        43		}
        44	
        45	}
    I work exclusively ( at this stage ) with currencies that make use of cents ( decimal ) and where the currency symbol is in front, so I don't need to over complicate things.

    Cheers

  3. #3
    Join Date
    Mar 2003
    Location
    Chennai, India
    Posts
    25
    Though Nico uses integers, it is better understood if used as decimal or float and then conversion is made. Perl does not impose any restrictions over decimal and it can be used as well.

    Float datatype preserves accuracy upto 24 digits.

    Refer this link for more details:
    http://www.mysql.com/doc/Numeric_types.html

    ------Karthikeyan

  4. #4
    Join Date
    Feb 2003
    Location
    Johannesburg, South Africa
    Posts
    145
    http://www.mysql.com/doc/Numeric_types.html - 404
    Rather try: http://www.mysql.com/doc/en/Numeric_types.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •