nVelocity - 尝试“大于”时出现模板问题小数性质的比较

发布于 2024-08-26 04:32:07 字数 709 浏览 8 评论 0原文

我有一个简单的对象,它的属性之一是名为 Amount 的小数。当我尝试将此属性作为 nVelocity 模板的一部分进行比较时,比较总是失败。如果我将属性更改为 int 类型,则比较效果很好。我需要在模板中添加任何额外内容才能进行比较吗?

下面是上述模板的示例:

#foreach( $bet in $bets )
<li>
 $bet.Date $bet.Race 
 #if($bet.Amount > 10)
  <strong>$bet.Amount.ToString("c")</strong>
 #else
  $bet.Amount.ToString("c")
 #end
</li>
#end

下面是 Bet 类:

public class Bet
{
    public Bet(decimal amount, string race, DateTime date)
    {
       Amount = amount;
       Race = race;
       Date = date;
    }

    public decimal Amount { get; set; }
    public string Race { get; set; }
    public DateTime Date { get; set; }
}

任何帮助将不胜感激。

I have a simple object that has as one of it's properties a decimal named Amount. When I attempt a comparison on this property as part of an nVelocity template, the comparison always fails. If I change the property to be of type int the comparison works fine. Is there anything extra I need to add to the template for the comparison to work?

Below is a sample from the aforementioned template:

#foreach( $bet in $bets )
<li>
 $bet.Date $bet.Race 
 #if($bet.Amount > 10)
  <strong>$bet.Amount.ToString("c")</strong>
 #else
  $bet.Amount.ToString("c")
 #end
</li>
#end

Below is the Bet class:

public class Bet
{
    public Bet(decimal amount, string race, DateTime date)
    {
       Amount = amount;
       Race = race;
       Date = date;
    }

    public decimal Amount { get; set; }
    public string Race { get; set; }
    public DateTime Date { get; set; }
}

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如梦亦如幻 2024-09-02 04:32:07

我测试了这个并且它有效。看来这是一个错误,在最新版本的 NVelocity(撰写本文时为 1.1)中不再存在。

I tested this and it worked. It seems this was a bug that isn't present any more in the latest release of NVelocity (1.1 as of this writing).

尾戒 2024-09-02 04:32:07

当然。

完整的 nVelocity 模板:

<div>
Bet summary:

<ul>
#foreach( $bet in $bets )
<li>
    $bet.Date $bet.Race 
    #if($bet.Amount > 10)
        <strong>$bet.Amount.ToString("c")</strong>
    #else
        $bet.Amount.ToString("c")
    #end
</li>
#end
</ul>

</div> 

Bet 类:

public class Bet
{
    public Bet(decimal amount, string race, DateTime date)
    {
       Amount = amount;
       Race = race;
       Date = date;
    }

    public decimal Amount { get; set; }
    public string Race { get; set; }
    public DateTime Date { get; set; }
}

程序:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Commons.Collections;
using NVelocity.App;
using NVelocity;
using NVelocity.Runtime;

namespace nVelocityTest
{
    public class Program
    {
        private static void Init()
        {
            var props = new ExtendedProperties();
            props.AddProperty(RuntimeConstants_Fields.FILE_RESOURCE_LOADER_PATH, @"C:\dev\RnD\nVelocity\nVelocityTest\nVelocityTest\EmailTemplates");
            Velocity.Init(props);
        }

        static void Main()
        {
            Init();

            ICollection<Bet> bet = new Collection<Bet> { new Bet(10, "Banana Race", DateTime.Now), new Bet(15, "Potatoe Race", DateTime.Now) };

            GenerateBetSummaryEmail(bet);
        }

        private static void GenerateBetSummaryEmail(ICollection<Bet> bets)
        {
            var context = new VelocityContext();
            context.Put("bets", bets);

            var writer = new System.IO.StringWriter();

            try
            {
                Velocity.MergeTemplate("BetConfirmationEmailTemplate.vm", context, writer);
            }
            catch (Exception e)
            {
                Console.WriteLine("Problem merging template : " + e);
            }

            var outputTest = writer.GetStringBuilder().ToString();
        } 
    }
}

预期输出测试:

以下是您的投注摘要:

  • 25/03/2010 9:21:15 上午
    香蕉竞赛 $10.00
  • 25/03/2010 上午 9:21:15 土豆比赛
    $15.00

实际产出测试:

以下是您的投注摘要:

  • 25/03/2010 9:21:15 上午
    香蕉竞赛 $10.00
  • 25/03/2010 上午 9:21:15 土豆比赛
    $15.00

如前所述,即使在第二个下注对象中,bet.Amount 的值为 15,nVelocity 模板中的比较 #if($bet.Amount > 10) 也会失败。如果 Amount 更改为 int 类型,比较按预期进行。

Sure.

The complete nVelocity template:

<div>
Bet summary:

<ul>
#foreach( $bet in $bets )
<li>
    $bet.Date $bet.Race 
    #if($bet.Amount > 10)
        <strong>$bet.Amount.ToString("c")</strong>
    #else
        $bet.Amount.ToString("c")
    #end
</li>
#end
</ul>

</div> 

The Bet class:

public class Bet
{
    public Bet(decimal amount, string race, DateTime date)
    {
       Amount = amount;
       Race = race;
       Date = date;
    }

    public decimal Amount { get; set; }
    public string Race { get; set; }
    public DateTime Date { get; set; }
}

Program:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Commons.Collections;
using NVelocity.App;
using NVelocity;
using NVelocity.Runtime;

namespace nVelocityTest
{
    public class Program
    {
        private static void Init()
        {
            var props = new ExtendedProperties();
            props.AddProperty(RuntimeConstants_Fields.FILE_RESOURCE_LOADER_PATH, @"C:\dev\RnD\nVelocity\nVelocityTest\nVelocityTest\EmailTemplates");
            Velocity.Init(props);
        }

        static void Main()
        {
            Init();

            ICollection<Bet> bet = new Collection<Bet> { new Bet(10, "Banana Race", DateTime.Now), new Bet(15, "Potatoe Race", DateTime.Now) };

            GenerateBetSummaryEmail(bet);
        }

        private static void GenerateBetSummaryEmail(ICollection<Bet> bets)
        {
            var context = new VelocityContext();
            context.Put("bets", bets);

            var writer = new System.IO.StringWriter();

            try
            {
                Velocity.MergeTemplate("BetConfirmationEmailTemplate.vm", context, writer);
            }
            catch (Exception e)
            {
                Console.WriteLine("Problem merging template : " + e);
            }

            var outputTest = writer.GetStringBuilder().ToString();
        } 
    }
}

Expected outputTest:

Below is your bet summary:

  • 25/03/2010 9:21:15 AM
    Banana Race $10.00
  • 25/03/2010 9:21:15 AM Potatoe Race
    $15.00

Actual outputTest:

Below is your bet summary:

  • 25/03/2010 9:21:15 AM
    Banana Race $10.00
  • 25/03/2010 9:21:15 AM Potatoe Race
    $15.00

As previously mentioned, the comparison #if($bet.Amount > 10) in the nVelocity template fails even though, in the second bet object, the value of bet.Amount is 15. If Amount is changed to be of type int, the comparison works as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文