簡體   English   中英

顯示 HTML 內的數據時舍入到小數點后 1

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

我正在創建一個基本的 web 應用程序,它在 HTML 表中顯示一些計算的結果。

現在我只想顯示 1 個小數位,但同時在后端保留 memory 中的所有小數。

<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定義或注冊有問題。 試圖自己弄清楚,但沒有有用的結果。

在一個暫定的解決方案中,我還將@Bean 放在 public String formatNumber(double number) 之前,但是該項目甚至沒有部署在我的 Tomcat 本地服務器上,給出錯誤“考慮在您的配置中定義一個 'double' 類型的 bean”。

假設這個問題是關於使用 thymeleaf 執行此操作:

注冊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>

也許你的意思是這個

$("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