Converting number abbreviations (5.2k, 1.7m, etc) into valid integers with PHP

后端 未结 6 997
执笔经年
执笔经年 2021-01-02 22:02

I have a database with a column containing a variety of numbers written in \"shorthand\", for example:

5k for 5,000
86.6k for 86,600
4.1m for 4,100,000
1

6条回答
  •  Happy的楠姐
    2021-01-02 22:42

    Logic to implement, iterate through all the characters, and when you find a character (not digit) just save the index. Now split that into two parts: digital value and character. Now create a function which returns the value of passed character. eg. getValueOf('M')=1000000 And then finally do this Numerical Value * getValueOf('M') That's all. Note: Your numerical values have commas in it so also do str_replace(',','',numeric value) and then convert to int

提交回复
热议问题