Welcome Guest, Not a member yet? Register   Sign In
Floating Point Numbers Problem (?)
#1

[eluser]Unknown[/eluser]
Hello to all of you!

First of all, I want to thank the CodeIgniter-Team to make this awesome and easy to use framework available for free!

I am a German web-developer (or about to become one) and currently working on a project using CI. The web-based application I am creating is going to be used by people who do not have much experience with personal computers and are ... well, not easy to convice to do anything against their habits ;-)

I have a little problem and do not know how to solve it (it's not entirely CI-related): The application I'm building has multiple forms on multiple pages to input data, which is then processed and stored in a (MySQL-based) database. In Germany, the common way to write/type floating point numbers (e.g. '3.456') is with a comma, like '3,456'. My question is, how do I work with float integers (not the correct term, I guess) and how can I let PHP do math with them? How do I process input data typed the 'German way'? E.g. if someone enters, let's say, a price of $34,95 ('$34.95'), what would be the best possible way to ensure a) PHP can work with this number and do maths, b) PHP/CI/MySQL won't crash and c) output the data (on other pages) in the 'German way'?

I'd appreciate you help and advice a lot! Thanks in advance and sorry for my bad English ;-)

Best regards,
Nico

p.s., I hope this is the right subforum ...
#2

[eluser]danmontgomery[/eluser]
You can use setlocale() to change the output of decimals, but I'm not aware of any way to change what PHP will take as decimal input. The only thing I can think of is to replace any ,s with .s before doing any work on them.

Code:
$str = str_replace(',','.',$str);
#3

[eluser]stommert[/eluser]
You know storing decimal numbers (currency values) in a floating point is a very bad idea?
please consider this thread, before storing currency values in a floating point field.
#4

[eluser]Unknown[/eluser]
I didn't know that, thanks for redirecting me to that thread =) Well, I think I'm going to use cent values for pricing calculations and gramme for weight calculations. Basically, all I needed would be functions for prices/weight which return cent/gramme values or €/$/kg values depending on input, right? The actual values would be stored as integers in the database.

Thanks a lot again!
#5

[eluser]stommert[/eluser]
It is my personal preference to use cent/gramme fields. I know some developers use a decimal field. As long as you keep it consistent you'll be fine.
It's always nice preventing someone from the potential horror of a floating point field for currency values.

So you're welcome.




Theme © iAndrew 2016 - Forum software by © MyBB