From 14f0d38cac7068496f45c8d1b0b61f8c795dfcff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=80=90=E9=9B=81=E5=8D=97=E9=A3=9B?= Date: Thu, 1 Jul 2021 13:59:00 +0800 Subject: [PATCH] Fixed an issue with round (#207) * Fix the problem that the result has a decimal point when the round function, when the precision is 0. * fix tests/test-functions.cpp:91 for round check --- include/inja/renderer.hpp | 6 +++++- single_include/inja/inja.hpp | 6 +++++- test/test-functions.cpp | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/include/inja/renderer.hpp b/include/inja/renderer.hpp index ef951b99..e299db58 100644 --- a/include/inja/renderer.hpp +++ b/include/inja/renderer.hpp @@ -439,7 +439,11 @@ class Renderer : public NodeVisitor { const auto args = get_arguments<2>(node); const int precision = args[1]->get(); const double result = std::round(args[0]->get() * std::pow(10.0, precision)) / std::pow(10.0, precision); - result_ptr = std::make_shared(std::move(result)); + if(0==precision){ + result_ptr = std::make_shared(int(result)); + }else{ + result_ptr = std::make_shared(std::move(result)); + } json_tmp_stack.push_back(result_ptr); json_eval_stack.push(result_ptr.get()); } break; diff --git a/single_include/inja/inja.hpp b/single_include/inja/inja.hpp index 03e19a06..509f5e7f 100644 --- a/single_include/inja/inja.hpp +++ b/single_include/inja/inja.hpp @@ -3935,7 +3935,11 @@ class Renderer : public NodeVisitor { const auto args = get_arguments<2>(node); const int precision = args[1]->get(); const double result = std::round(args[0]->get() * std::pow(10.0, precision)) / std::pow(10.0, precision); - result_ptr = std::make_shared(std::move(result)); + if(0==precision){ + result_ptr = std::make_shared(int(result)); + }else{ + result_ptr = std::make_shared(std::move(result)); + } json_tmp_stack.push_back(result_ptr); json_eval_stack.push(result_ptr.get()); } break; diff --git a/test/test-functions.cpp b/test/test-functions.cpp index 4ec4c25b..dab8e9b5 100644 --- a/test/test-functions.cpp +++ b/test/test-functions.cpp @@ -88,7 +88,7 @@ TEST_CASE("functions") { } SUBCASE("round") { - CHECK(env.render("{{ round(4, 0) }}", data) == "4.0"); + CHECK(env.render("{{ round(4, 0) }}", data) == "4"); CHECK(env.render("{{ round(temperature, 2) }}", data) == "25.68"); // CHECK_THROWS_WITH( env.render("{{ round(name, 2) }}", data), "[inja.exception.json_error] // [json.exception.type_error.302] type must be number, but is string" );