Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

php - How to store European Currency in MySQL?

On a new project I work on I have data in CSV format to import into a mysql table. One of the columns is a price field which stores currency in the european format ie. 345,83. The isssue I have is storing this decimal seperator. In most European currencies the decimal seperator is "," but when I try to insert a decimal number into a field (ex. 345,83), I get the following error: "Data truncated for column 'column_name' at row 'row #'". If I use '.' instead of ',' it works fine. Could you please help me with, how to store this format in mysql?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you can store it as a regular decimal field in the database, and format the number european style when you display it

edit: just added an example of how it might be achieved

$european_numbers = array('123.345,78', '123 456,78', ',78');

foreach($european_numbers as $number) {

    echo "$number was converted to ".convert_european_to_decimal($number)."
";
    // save in database now
}

function convert_european_to_decimal($number) {
    // i am sure there are better was of doing this, but this is nice and simple example
    $number = str_replace('.', '', $number); // remove fullstop
    $number = str_replace(' ', '', $number); // remove spaces
    $number = str_replace(',', '.', $number); // change comma to fullstop

    return $number;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...