【问题标题】:Round to 1 decimal when displaying data within HTML在 HTML 中显示数据时舍入到小数点后 1
【发布时间】:2021-05-25 01:03:39
【问题描述】:

我正在创建一个基本的 Web 应用程序,它在 HTML 表格中显示某些计算的结果。

现在我只想显示 1 位小数,但同时将计算保留在后端,将所有小数保留在内存中。

<tr th:each="value : ${allvalues}">
    <td  th:text="${value.firstNumber}" ></td>
    <td  th:text="${value.secondNumber}" ></td>
</tr>

我尝试使用 parseFloat(${value.firstNumber}).toFixed(1) 但它不起作用。 firstNumber 和 secondNumber 是 double 类型

我想我错过了一些非常简单但无法弄清楚的东西。

更新 1: 我正在尝试按照@mehowthe 的建议使用 bean。

这是我放置 bean 的 SpringBootWebApplication 的代码:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;


import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@SpringBootApplication
@Configuration
@ComponentScan(basePackageClasses = SpringBootWebApplication.class, useDefaultFilters = true)
public class SpringBootWebApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootWebApplication.class);
    }

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext context = SpringApplication.run(SpringBootWebApplication.class, args);
        MathUtils mathUtils = context.getBean(MathUtils.class);

    }

    @Component
    public static class MathUtils{

         public String formatNumber(double number) {
          // or any other way to get 1 decimal place
             return new DecimalFormat("#.#").format(number); 
       } 
    }
}

访问包含 HTML 表的页面时出现的错误是:

2021-02-23 12:23:18.458 ERROR 22364 --- [apr-8080-exec-6] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-apr-8080-exec-6] Exception processing template "index": Exception evaluating SpringEL expression: "@mathUtils.formatNumber(value.firstNumber)" (index:187)
Caused by: org.springframework.expression.AccessException: Could not resolve bean reference against BeanFactory
        at org.springframework.context.expression.BeanFactoryResolver.resolve(BeanFactoryResolver.java:48) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
        at org.springframework.expression.spel.ast.BeanReference.getValueInternal(BeanReference.java:55) ~[spring-expression-4.3.8.RELEASE.jar:4.3.8.RELEASE]
        ... 100 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'mathUtils' available
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1207) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]

可能是 bean 定义或注册有问题。试图自己弄清楚,但没有任何有用的结果。

在一个暂定的解决方案中,我还在 public String formatNumber(double number) 之前放置了@Bean,但是该项目甚至没有部署在我的 Tomcat 本地服务器上,给出错误“考虑在配置中定义一个 'double' 类型的 bean 。”

【问题讨论】:

  • 请显示呈现的 HTML 和值。您没有向我们展示您何时以及如何解析浮动它们
  • 这应该有javascript标签而不是java标签。 Java 与 javascript 没有关系。

标签: java html jquery web-applications


【解决方案1】:

假设这个问题是关于用百里香做这个的:

注册bean:

@SpringBootApplication
public class StartWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(StartWebApplication.class, args);
    }

    @Bean
    public MathUtils mathUtils() {
        return new MathUtils();
    }

    public static class MathUtils {

        public String formatNumber(double number) {
            // or any other way to get 1 decimal place
            return new DecimalFormat("#.#").format(number);
        }
    }
}

在模板中使用:

<tr th:each="value : ${allvalues}">
    <td  th:text="${@mathUtils.formatNumber(value.firstNumber)}" ></td>
    <td  th:text="${@mathUtils.formatNumber(value.secondNumber)}" ></td>
</tr>

【讨论】:

  • 是的。问题是关于用百里香叶做的。抱歉没有具体说明。我现在遇到了 bean 配置的问题。我已经用我放置你的 bean 的代码的 sn-p 更新了我的问题。
  • @Component 应该可以工作,如果你把它放在单独的文件中。我在编辑示例中添加了如何在 StartWebApplication 主类中执行此操作
【解决方案2】:

也许你是这个意思

$("th").each(function() {
  const val = this.textContent;
  if (!isNaN(val)) this.textContent = Number(val).toFixed(1)
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    相关资源
    最近更新 更多