Skip to content

Implement decimal to DECIMAL conversion on non-.NET platforms #1455

@AArnott

Description

@AArnott

The .NET decimal type doesn't expose a proper API for extracting raw values without allocating an int[] unless you target .NET.
But .NET Standard targets can do this too by leveraging the field layout of the struct being intentionally the same between the two.

namespace Windows.Win32.Foundation
{
	/// <content>
	/// Adds implicit conversion from decimal to DECIMAL.
	/// </content>
	internal partial struct DECIMAL
	{
		public static unsafe implicit operator DECIMAL(decimal value)
		{
			// DECIMAL is layout-compatible with decimal.
			return *(DECIMAL*)&value;
		}
	}
}

Also, the converter that is emitted is buggy:

public static implicit operator decimal(DECIMAL value)
{
	return new decimal(
(int)value.Anonymous2.Anonymous.Lo32, (int)value.Anonymous2.Anonymous.Mid32, (int)value.Hi32, value.Anonymous1.Anonymous.sign == 0x80, value.Anonymous1.Anonymous.scale);
}

This throws OverflowException as it casts uint fields to int values without unchecked.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions