如何格式化BigDecimal到ALWAYS有两个小数位?

时间:2017-01-11 19:40:28

标签: java number-formatting bigdecimal

此:

go install $(go list ./... | grep -v vendor/)

变为BigDecimal.valueOf(0.00)

我希望它是0.0

该格式的正确格式是什么?

2 个答案:

答案 0 :(得分:0)

使用String构造函数:@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class SimpleTest extends JerseyTest { @Override protected Application configure() { enable(TestProperties.LOG_TRAFFIC); enable(TestProperties.DUMP_ENTITY); ResourceConfig rc = new ResourceConfig(); rc.property("contextConfig", new AnnotationConfigApplicationContext(ResourceConfig.class)); rc.register(SpringLifecycleListener.class); rc.registerClasses(HelloResource.class); return rc; } public static class SimpleEntity { public String getOrgId() { return orgId; } public void setOrgId(final String orgId) { this.orgId = orgId; } public String getType() { return type; } public void setType(final String type) { this.type = type; } @HeaderParam("orgId") private String orgId; private String type; } @Path("hello") public static class HelloResource { @POST @Produces(MediaType.APPLICATION_JSON) public SimpleEntity getHello(@BeanParam SimpleEntity entity) { return entity; } } @Test public void test() { SimpleEntity requestEntity= new SimpleEntity(); requestEntity.setType("MY-TYPE"); final SimpleEntity entity = target("hello").request().header("orgId", "xx1").post(Entity.entity(requestEntity, MediaType.APPLICATION_JSON_TYPE), SimpleEntity.class); assertNotNull(entity.getOrgId()); assertNotNull(entity.getType()); } @Configuration protected static class BlankSpringConfig{} } 。使用new BigDecimal("0.00")会完全破坏用于输入值的任何格式。

答案 1 :(得分:0)

试试这个

value = value.setScale(2, RoundingMode.CEILING)