在DataWeave中格式化时截断数字

问题描述

在格式化数字时,有没有更短的截断数字的方法?看起来默认行为是四舍五入。现在我有了这个:

var val = 123.129
---
{
    def: val as String {format: "0.00"},truncate: 
        (if(val > 0) (floor(val * 100)/100)
        else (ceil(val * 100)/100)
        ) as String {format: "0.00"}
}

输出为

{
  "def": "123.13","truncate": "123.12"
}

解决方法

您可以在类型转换中定义舍入模式:

public class ViewModel
{
    public ViewModel(ILogger logger,...)
    {
        ThemeList = _userSettingsService.GetThemeList();

        // Configure the theme Apply button to both select and save the theme
        ThemeApplyAndSaveCommand.RegisterCommand(ThemeApplyCommand);
        ThemeApplyAndSaveCommand.RegisterCommand(ThemeSaveCommand);

        // Activate the user's preferred theme
        SelectedTheme = _userSettingsService.GetThemePreference(securityContext.User);
        ThemeApplyCommand.Execute();

        logger.Debug("    ThemeApplyCommand.CanExecute: {canExecute}",ThemeApplyCommand.CanExecute());
        logger.Debug("    ThemeSaveCommand.CanExecute: {canExecute}",ThemeSaveCommand.CanExecute());
        logger.Debug("    ThemeApplyAndSaveCommand.CanExecute: {canExecute}",ThemeApplyAndSaveCommand.CanExecute(null));
    }


    public CompositeCommand ThemeApplyAndSaveCommand => new CompositeCommand();

    public DelegateCommand ThemeApplyCommand => new DelegateCommand(ExecuteThemeApplyCommand);

    public DelegateCommand ThemeSaveCommand => new DelegateCommand(ExecuteThemeSaveCommand);

    private void ExecuteThemeApplyCommand()
    {
        ...
    }

    private void ExecuteThemeSaveCommand()
    {
        ...
    }
}

这些模式基于https://docs.oracle.com/javase/8/docs/api/java/math/RoundingMode.html的名称。

另一种选择是执行您所做的操作,但将其封装为可重用的函数:

down: 123.129 as String {format: "0.00",roundMode:"DOWN" }

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...