如何解决在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" }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。