【问题标题】:Is there an equivalent to Python's Format Specification Mini-Language in JavaScript/Vue?在 JavaScript/Vue 中是否有相当于 Python 的格式规范迷你语言?
【发布时间】:2021-08-10 11:13:28
【问题描述】:

我正在开发一个 Django 项目,该项目通过矢量图块将数据传送到 Vue(tify) 前端。由于这种架构,在 Django 模型实例上运行 Python 是不切实际的。矢量切片在数据库内部生成,查询集未在 Django 后端实现。

此外,我们宁愿以原始类型传送数据并在前端处理格式。 从 Python 我知道Format Specification Mini-Language。 JavaScript/Vue/Vuetify 中是否有等价物?


示例:在 Python 中,f"{1/9:.4f}" 将导致 '0.1111'


相关:此回答JavaScript equivalent to printf/String.Format。我正在寻找更灵活的东西,就像 Format Specification Mini-Language

【问题讨论】:

    标签: javascript node.js vue.js formatting vuetify.js


    【解决方案1】:

    最好和最灵活的方法应该是 Inlt,一个带有 大量格式化程序 用于各种数据类型的 js 命名空间。

    一些例子:

    new Intl.NumberFormat().format(12345)
    // 12,345
    
    new Intl.NumberFormat('en-US', { maximumSignificantDigits: 4}).format(1.2345678)
    // 1.235 (Notice the rounding)
    
    new Intl.NumberFormat('en-GB', { style: 'currency', currency: 'GBP' }).format(9002.20)
    // £9,002.20
    
    new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(9002.20)
    // 9.002,20 €
    
    new Intl.NumberFormat().formatToParts(12345.678)
    /*
    [
       { "type":"integer", "value":"12" },
       { "type":"group", "value":"," },
       { "type":"integer", "value":"345" },
       { "type":"decimal", "value":"." },
       { "type":"fraction", "value":"678" }
    ]
    

    (ref)

    new Intl.DateTimeFormat('default', {
      hour: 'numeric',
      minute: 'numeric',
      second: 'numeric'
    }).format(date)
    // → '2:00:00 pm
    
    new Intl.DateTimeFormat('en-US', {
      year: 'numeric',
      month: 'numeric',
      day: 'numeric'
    }).format(date)
    // → '12/19/2012'
    
    

    (ref)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-24
      • 2012-09-21
      • 2011-08-23
      • 2022-01-13
      • 1970-01-01
      相关资源
      最近更新 更多