简体   繁体   English

显示 HTML 内的数据时舍入到小数点后 1

[英]Round to 1 decimal when displaying data within HTML

I am creating a basic web application that dislays the results of some calculation in a HTML table.我正在创建一个基本的 web 应用程序,它在 HTML 表中显示一些计算的结果。

Now i would like to show only 1 decimal place, but at the same time keep the calculation on the back end keeping all the decimals in memory.现在我只想显示 1 个小数位,但同时在后端保留 memory 中的所有小数。

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

I tried to use parseFloat(${value.firstNumber}).toFixed(1) but it is not working.我尝试使用 parseFloat(${value.firstNumber}).toFixed(1) 但它不起作用。 firstNumber and secondNumber are of type double firstNumber 和 secondNumber 是 double 类型

I think i am missing something very simple but cannot figure it out.我想我错过了一些非常简单但无法弄清楚的东西。

UPDATE 1: I am trying to use the bean as per @mehowthe suggestion.更新 1:我正在尝试按照 @mehowthe 的建议使用 bean。

Here is the code of my SpringBootWebApplication where i placed the 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); 
       } 
    }
}

The error when i get when accessing the page containing the HTML Table is this one:访问包含 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]

Presumibly there is something wrong with the bean definition or registration.大概是bean定义或注册有问题。 Tried to figure out by myself but with no useful results.试图自己弄清楚,但没有有用的结果。

In a tentative solution I also placed @Bean before public String formatNumber(double number) but then the project was not even getting deployed on my Tomcat local server giving the error "Consider defining a bean of type 'double' in your configuration."在一个暂定的解决方案中,我还将@Bean 放在 public String formatNumber(double number) 之前,但是该项目甚至没有部署在我的 Tomcat 本地服务器上,给出错误“考虑在您的配置中定义一个 'double' 类型的 bean”。

Assuming that this question is about doing this with thymeleaf:假设这个问题是关于使用 thymeleaf 执行此操作:

Register bean:注册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);
        }
    }
}

Use in template:在模板中使用:

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

Perhaps you mean this也许你的意思是这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM