Skip to content

Commit ae97611

Browse files
committed
improve: prices now can use a decimal comma as a separator
Fix #1513
1 parent d75c7b8 commit ae97611

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/main/java/ru/mystamps/web/config/MvcConfig.java

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import ru.mystamps.web.feature.site.ResourceUrl;
6262
import ru.mystamps.web.feature.site.SiteConfig;
6363
import ru.mystamps.web.feature.site.SiteUrl;
64+
import ru.mystamps.web.support.spring.mvc.BigDecimalConverter;
6465
import ru.mystamps.web.support.spring.mvc.RestExceptionHandler;
6566
import ru.mystamps.web.support.spring.security.CurrentUserArgumentResolver;
6667

@@ -95,6 +96,7 @@ public class MvcConfig implements WebMvcConfigurer {
9596
public void addFormatters(FormatterRegistry registry) {
9697
registry.addConverter(new CategoryLinkEntityDtoConverter(categoryService));
9798
registry.addConverter(new CountryLinkEntityDtoConverter(countryService));
99+
registry.addConverter(new BigDecimalConverter());
98100
}
99101

100102
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (C) 2009-2021 Slava Semushin <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
19+
package ru.mystamps.web.support.spring.mvc;
20+
21+
import org.apache.commons.lang3.StringUtils;
22+
import org.springframework.core.convert.converter.Converter;
23+
24+
import java.math.BigDecimal;
25+
26+
// @todo #1513 Add integration test to check that prices accept a decimal comma
27+
// CheckStyle: ignore LineLength for next 2 lines
28+
/**
29+
* Converter for BigDecimal that correctly parse values with a comma separator (in addition to a point).
30+
*/
31+
public class BigDecimalConverter implements Converter<String, BigDecimal> {
32+
33+
@Override
34+
public BigDecimal convert(String source) {
35+
if (StringUtils.EMPTY.equals(source)) {
36+
return null;
37+
}
38+
39+
String value = source;
40+
if (source.indexOf(',') >= 0) {
41+
// "10,5" => "10.5"
42+
value = source.replace(',', '.');
43+
}
44+
45+
return new BigDecimal(value);
46+
}
47+
48+
}

0 commit comments

Comments
 (0)