RoundDecimal.Fody
1.0.5
dotnet add package RoundDecimal.Fody --version 1.0.5
NuGet\Install-Package RoundDecimal.Fody -Version 1.0.5
<PackageReference Include="RoundDecimal.Fody" Version="1.0.5"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="RoundDecimal.Fody" Version="1.0.5" />
<PackageReference Include="RoundDecimal.Fody"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add RoundDecimal.Fody --version 1.0.5
#r "nuget: RoundDecimal.Fody, 1.0.5"
#:package RoundDecimal.Fody@1.0.5
#addin nuget:?package=RoundDecimal.Fody&version=1.0.5
#tool nuget:?package=RoundDecimal.Fody&version=1.0.5
RoundDecimal.Fody
A Fody add-in that automatically rounds decimal properties to a specified precision. This allows you to enforce rounding rules directly on your model properties.
How To Use
1. Install the NuGet Package
Install the RoundDecimal.Fody package into your project from NuGet.
This will add the package and automatically create a FodyWeavers.xml file in your project root.
2. Add the [RoundDecimal] Attribute
Decorate any decimal or decimal? property with the [RoundDecimal] attribute. The attribute takes a single integer argument, which is the number of decimal places to round to.
## RoundDecimal.Fody
[](https://badge.fury.io/nu/RoundDecimal.Fody)
RoundDecimal.Fody is a Fody weaver that automatically rounds assigned values for `decimal` and `decimal?` properties to a configured precision at compile time. Use it to enforce consistent rounding rules in your domain models without manual rounding logic.
## Quick start
1. Add the NuGet package to your project (PackageReference):
```xml
<PackageReference Include="RoundDecimal.Fody" Version="1.0.0" />
<PackageReference Include="Fody" Version="6.*" PrivateAssets="all" />
- Ensure a
FodyWeavers.xmlfile exists in your project root and containsRoundDecimal.Fody:
<?xml version="1.0" encoding="utf-8" ?>
<Weavers>
<RoundDecimal.Fody />
</Weavers>
- Add the attribute to your properties:
using RoundDecimal.Fody;
public class Invoice
{
// Rounded to 2 decimal places
[RoundDecimal(2)]
public decimal? Total { get; set; }
// Rounded to 3 decimal places
[RoundDecimal(3)]
public decimal Fee { get; set; }
}
The weaver injects rounding logic into the property setter so the value stored in the backing field is the rounded result. Nullable properties (decimal?) are left null when null is assigned; non-null values are rounded.
Behavior example
Before weaving (your source):
var invoice = new Invoice();
invoice.Total = 123.4567m; // source assignment
After weaving (what runs at runtime):
// the value stored in the backing field will be rounded to the configured precision
invoice.Total == 123.46m; // if precision = 2
Compatibility
- Targets typical .NET SDK projects (netstandard / net5+/net6+/net7+/net8+/net9+ as supported by Fody and your environment). Test against the target framework you use.
Publishing (for maintainers)
- Build and pack the weaver project (it is distributed as a NuGet package with a
weaver/assembly):
dotnet build
dotnet pack -c Release
- Push to NuGet.org (use your API key and the correct package path):
dotnet nuget push "nupkgs/RoundDecimal.Fody.*.nupkg" --api-key <API_KEY> --source https://api.nuget.org/v3/index.json
Note: Fody weavers expect the weaver assembly to be placed in the package weaver/ folder so Fody can find and run it during build. See the Fody docs for packaging guidelines.
Troubleshooting
- TypeLoadException related to
System.Nullable\1` often indicates the weaver injected references that don't match the target assembly scope. The weaver attempts to import references using the target module; if you see errors, please open an issue with a small repro (target framework and a minimal class with the attribute). - If the weaver does not run, ensure
Fodyis installed andFodyWeavers.xmlcontains the weaver entry.
Contributing
Contributions are welcome. Please open issues or pull requests with clear repros and tests.
License
Specify your license here (e.g., MIT). If you have a LICENSE file, reference it.
If you'd like, I can also add a short example project showing the before/after or add automated tests that validate rounding behavior during build.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Fody (>= 6.8.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.