douqiang6448 2014-07-23 20:37
浏览 29
已采纳

Wordpress + ACF - 在输入上格式化数字(在后端)

I have this Custom Posts with several number fields in it, i'm looking forward to format the numbers given(in the edit post in the backend for the admin) immediatly after presented, in a money format, that means, the number field should accept commas too. I'm not sure on how to start, have any of you done something similiar?

Please help I'm kinda new to wordpress

  • 写回答

1条回答 默认 最新

  • douxing6532 2014-07-23 21:07
    关注

    Have a look at number_format.

    Here's an example of dealing with numeric and non-numeric values. Uncomment //$unformatted = "13,009"; to see the string version in action.

    Hopefully this is all enough to point you in the right direction.

    // Different versions of a number
    $unformatted = "13009";
    //$unformatted = "13,009";
    
    // If the variable is a number
    if (is_numeric ( $unformatted ) )
    {
        $formatted = number_format($unformatted);            // Apply number format
        $formattedDecimals = number_format($unformatted, 2); // Apply number format (with decimals)
        echo $formatted."<br />".$formattedDecimals;         // Output the values
    
    }
    
    // If the variable is text
    else
    {
        $integerValue = str_replace(",", "", $unformatted);  // Remove commas from string
        $integerValue = intval($integerValue);               // Convert from string to integer
        $integerValue = number_format($integerValue);        // Apply number format
    
        $floatValue = str_replace(",", "", $unformatted);    // Remove commas from string
        $floatValue = floatval($floatValue);                 // Convert from string to float
        $floatValue = number_format($floatValue, 2);         // Apply number format
    
        echo $integerValue."<br />".$floatValue;             // Output values
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料