显示 HTML 以内的数据时四舍五入为小数点后一位
Round to 1 decimal when displaying data within HTML
我正在创建一个基本的 Web 应用程序,它在 HTML table.
中显示某些计算的结果
现在我想只显示一位小数,但同时在后端进行计算,将所有小数保留在内存中。
<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 Table 的页面时出现的错误是:
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。"
也许你是这个意思
$("th").each(function() {
const val = this.textContent;
if (!isNaN(val)) this.textContent = Number(val).toFixed(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>
我正在创建一个基本的 Web 应用程序,它在 HTML table.
中显示某些计算的结果现在我想只显示一位小数,但同时在后端进行计算,将所有小数保留在内存中。
<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 Table 的页面时出现的错误是:
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。"
也许你是这个意思
$("th").each(function() {
const val = this.textContent;
if (!isNaN(val)) this.textContent = Number(val).toFixed(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>