Repository: bitshifter/glam-rs
Pull request #491 (2024-03-11 07:31:24)

Respect precision format specifier in Display implementations.


Patch:
diff --git a/codegen/templates/affine.rs.tera b/codegen/templates/affine.rs.tera
index fe9ac56..f9b5141 100644
--- a/codegen/templates/affine.rs.tera
+++ b/codegen/templates/affine.rs.tera
@@ -734,15 +734,27 @@ impl core::fmt::Debug for {{ self_t }} {
 #[cfg(not(target_arch = "spirv"))]
 impl core::fmt::Display for {{ self_t }} {
     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
-        {% if dim == 2 %}
-            write!(f, "[{}, {}, {}]", self.matrix2.x_axis, self.matrix2.y_axis, self.translation)
-        {% elif dim == 3 %}
-            write!(
-                f,
-                "[{}, {}, {}, {}]",
-                self.matrix3.x_axis, self.matrix3.y_axis, self.matrix3.z_axis, self.translation
-            )
-        {% endif %}
+        if let Some(p) = f.precision() {
+            {% if dim == 2 %}
+                write!(f,
+                       "[{:.*}, {:.*}, {:.*}]",
+                       p, self.matrix2.x_axis, p, self.matrix2.y_axis, p, self.translation)
+            {% elif dim == 3 %}
+                write!(f,
+                       "[{:.*}, {:.*}, {:.*}, {:.*}]",
+                       p, self.matrix3.x_axis, p, self.matrix3.y_axis, p, self.matrix3.z_axis, p, self.translation)
+            {% endif %}
+        } else {
+            {% if dim == 2 %}
+                write!(f, "[{}, {}, {}]", self.matrix2.x_axis, self.matrix2.y_axis, self.translation)
+            {% elif dim == 3 %}
+                write!(
+                    f,
+                    "[{}, {}, {}, {}]",
+                    self.matrix3.x_axis, self.matrix3.y_axis, self.matrix3.z_axis, self.translation
+                )
+            {% endif %}
+        }
     }
 }
 
diff --git a/codegen/templates/mat.rs.tera b/codegen/templates/mat.rs.tera
index 5b03978..c844ca0 100644
--- a/codegen/templates/mat.rs.tera
+++ b/codegen/templates/mat.rs.tera
@@ -2402,13 +2402,23 @@ impl fmt::Debug for {{ self_t }} {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for {{ self_t }} {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        {% if dim == 2 %}
-            write!(f, "[{}, {}]", self.x_axis, self.y_axis)
-        {% elif dim == 3 %}
-            write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
-        {% elif dim == 4 %}
-            write!(f, "[{}, {}, {}, {}]", self.x_axis, self.y_axis, self.z_axis, self.w_axis)
-        {% endif %}
+        if let Some(p) = f.precision() {
+            {% if dim == 2 %}
+                write!(f, "[{:.*}, {:.*}]", p, self.x_axis, p, self.y_axis)
+            {% elif dim == 3 %}
+                write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x_axis, p, self.y_axis, p, self.z_axis)
+            {% elif dim == 4 %}
+                write!(f, "[{:.*}, {:.*}, {:.*}, {:.*}]", p, self.x_axis, p, self.y_axis, p, self.z_axis, p, self.w_axis)
+            {% endif %}
+        } else {
+            {% if dim == 2 %}
+                write!(f, "[{}, {}]", self.x_axis, self.y_axis)
+            {% elif dim == 3 %}
+                write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
+            {% elif dim == 4 %}
+                write!(f, "[{}, {}, {}, {}]", self.x_axis, self.y_axis, self.z_axis, self.w_axis)
+            {% endif %}
+        }
     }
 }
 
diff --git a/codegen/templates/quat.rs.tera b/codegen/templates/quat.rs.tera
index 78db7d9..6c32624 100644
--- a/codegen/templates/quat.rs.tera
+++ b/codegen/templates/quat.rs.tera
@@ -1089,8 +1089,12 @@ impl fmt::Debug for {{ self_t }} {
 
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for {{ self_t }} {
-    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(fmt, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        if let Some(p) = f.precision() {
+            write!(f, "[{:.*}, {:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z, p, self.w)
+        } else {
+            write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
+        }
     }
 }
 
diff --git a/codegen/templates/vec.rs.tera b/codegen/templates/vec.rs.tera
index 1149948..489014a 100644
--- a/codegen/templates/vec.rs.tera
+++ b/codegen/templates/vec.rs.tera
@@ -2958,12 +2958,26 @@ impl IndexMut<usize> for {{ self_t }} {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for {{ self_t }} {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        {% if dim == 2 %}
-            write!(f, "[{}, {}]", self.x, self.y)
-        {% elif dim == 3 %}
-            write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
-        {% elif dim == 4 %}
-            write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
+        {% if is_float %}
+        if let Some(p) = f.precision() {
+            {% if dim == 2 %}
+                write!(f, "[{:.*}, {:.*}]", p, self.x, p, self.y)
+            {% elif dim == 3 %}
+                write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
+            {% elif dim == 4 %}
+                write!(f, "[{:.*}, {:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z, p, self.w)
+            {% endif %}
+        } else {
+        {% endif %}
+            {% if dim == 2 %}
+                write!(f, "[{}, {}]", self.x, self.y)
+            {% elif dim == 3 %}
+                write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
+            {% elif dim == 4 %}
+                write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
+            {% endif %}
+        {% if is_float %}
+        }
         {% endif %}
     }
 }
diff --git a/src/f32/affine2.rs b/src/f32/affine2.rs
index c089586..6e1c5b6 100644
--- a/src/f32/affine2.rs
+++ b/src/f32/affine2.rs
@@ -357,11 +357,19 @@ impl core::fmt::Debug for Affine2 {
 #[cfg(not(target_arch = "spirv"))]
 impl core::fmt::Display for Affine2 {
     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
-        write!(
-            f,
-            "[{}, {}, {}]",
-            self.matrix2.x_axis, self.matrix2.y_axis, self.translation
-        )
+        if let Some(p) = f.precision() {
+            write!(
+                f,
+                "[{:.*}, {:.*}, {:.*}]",
+                p, self.matrix2.x_axis, p, self.matrix2.y_axis, p, self.translation
+            )
+        } else {
+            write!(
+                f,
+                "[{}, {}, {}]",
+                self.matrix2.x_axis, self.matrix2.y_axis, self.translation
+            )
+        }
     }
 }
 
diff --git a/src/f32/affine3a.rs b/src/f32/affine3a.rs
index 1fdddd5..a02e392 100644
--- a/src/f32/affine3a.rs
+++ b/src/f32/affine3a.rs
@@ -499,11 +499,26 @@ impl core::fmt::Debug for Affine3A {
 #[cfg(not(target_arch = "spirv"))]
 impl core::fmt::Display for Affine3A {
     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
-        write!(
-            f,
-            "[{}, {}, {}, {}]",
-            self.matrix3.x_axis, self.matrix3.y_axis, self.matrix3.z_axis, self.translation
-        )
+        if let Some(p) = f.precision() {
+            write!(
+                f,
+                "[{:.*}, {:.*}, {:.*}, {:.*}]",
+                p,
+                self.matrix3.x_axis,
+                p,
+                self.matrix3.y_axis,
+                p,
+                self.matrix3.z_axis,
+                p,
+                self.translation
+            )
+        } else {
+            write!(
+                f,
+                "[{}, {}, {}, {}]",
+                self.matrix3.x_axis, self.matrix3.y_axis, self.matrix3.z_axis, self.translation
+            )
+        }
     }
 }
 
diff --git a/src/f32/coresimd/mat2.rs b/src/f32/coresimd/mat2.rs
index f4fd04f..ce2ccd2 100644
--- a/src/f32/coresimd/mat2.rs
+++ b/src/f32/coresimd/mat2.rs
@@ -530,6 +530,10 @@ impl fmt::Debug for Mat2 {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Mat2 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "[{}, {}]", self.x_axis, self.y_axis)
+        if let Some(p) = f.precision() {
+            write!(f, "[{:.*}, {:.*}]", p, self.x_axis, p, self.y_axis)
+        } else {
+            write!(f, "[{}, {}]", self.x_axis, self.y_axis)
+        }
     }
 }
diff --git a/src/f32/coresimd/mat3a.rs b/src/f32/coresimd/mat3a.rs
index 927ab2b..4e87f36 100644
--- a/src/f32/coresimd/mat3a.rs
+++ b/src/f32/coresimd/mat3a.rs
@@ -795,6 +795,14 @@ impl fmt::Debug for Mat3A {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Mat3A {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
+        if let Some(p) = f.precision() {
+            write!(
+                f,
+                "[{:.*}, {:.*}, {:.*}]",
+                p, self.x_axis, p, self.y_axis, p, self.z_axis
+            )
+        } else {
+            write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
+        }
     }
 }
diff --git a/src/f32/coresimd/mat4.rs b/src/f32/coresimd/mat4.rs
index 8a18b8c..04f9b8e 100644
--- a/src/f32/coresimd/mat4.rs
+++ b/src/f32/coresimd/mat4.rs
@@ -1451,10 +1451,18 @@ impl fmt::Debug for Mat4 {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Mat4 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(
-            f,
-            "[{}, {}, {}, {}]",
-            self.x_axis, self.y_axis, self.z_axis, self.w_axis
-        )
+        if let Some(p) = f.precision() {
+            write!(
+                f,
+                "[{:.*}, {:.*}, {:.*}, {:.*}]",
+                p, self.x_axis, p, self.y_axis, p, self.z_axis, p, self.w_axis
+            )
+        } else {
+            write!(
+                f,
+                "[{}, {}, {}, {}]",
+                self.x_axis, self.y_axis, self.z_axis, self.w_axis
+            )
+        }
     }
 }
diff --git a/src/f32/coresimd/quat.rs b/src/f32/coresimd/quat.rs
index 0e4e703..ad6762c 100644
--- a/src/f32/coresimd/quat.rs
+++ b/src/f32/coresimd/quat.rs
@@ -746,8 +746,16 @@ impl fmt::Debug for Quat {
 
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Quat {
-    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(fmt, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        if let Some(p) = f.precision() {
+            write!(
+                f,
+                "[{:.*}, {:.*}, {:.*}, {:.*}]",
+                p, self.x, p, self.y, p, self.z, p, self.w
+            )
+        } else {
+            write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
+        }
     }
 }
 
diff --git a/src/f32/coresimd/vec3a.rs b/src/f32/coresimd/vec3a.rs
index 338b6a1..52f58be 100644
--- a/src/f32/coresimd/vec3a.rs
+++ b/src/f32/coresimd/vec3a.rs
@@ -1157,7 +1157,11 @@ impl IndexMut<usize> for Vec3A {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Vec3A {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
+        if let Some(p) = f.precision() {
+            write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
+        } else {
+            write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
+        }
     }
 }
 
diff --git a/src/f32/coresimd/vec4.rs b/src/f32/coresimd/vec4.rs
index 57dd9a7..59b11eb 100644
--- a/src/f32/coresimd/vec4.rs
+++ b/src/f32/coresimd/vec4.rs
@@ -1068,7 +1068,15 @@ impl IndexMut<usize> for Vec4 {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Vec4 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
+        if let Some(p) = f.precision() {
+            write!(
+                f,
+                "[{:.*}, {:.*}, {:.*}, {:.*}]",
+                p, self.x, p, self.y, p, self.z, p, self.w
+            )
+        } else {
+            write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
+        }
     }
 }
 
diff --git a/src/f32/mat3.rs b/src/f32/mat3.rs
index 0203231..24c5ee9 100644
--- a/src/f32/mat3.rs
+++ b/src/f32/mat3.rs
@@ -805,6 +805,14 @@ impl fmt::Debug for Mat3 {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Mat3 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
+        if let Some(p) = f.precision() {
+            write!(
+                f,
+                "[{:.*}, {:.*}, {:.*}]",
+                p, self.x_axis, p, self.y_axis, p, self.z_axis
+            )
+        } else {
+            write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
+        }
     }
 }
diff --git a/src/f32/scalar/mat2.rs b/src/f32/scalar/mat2.rs
index 8ab90aa..262c13d 100644
--- a/src/f32/scalar/mat2.rs
+++ b/src/f32/scalar/mat2.rs
@@ -511,6 +511,10 @@ impl fmt::Debug for Mat2 {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Mat2 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "[{}, {}]", self.x_axis, self.y_axis)
+        if let Some(p) = f.precision() {
+            write!(f, "[{:.*}, {:.*}]", p, self.x_axis, p, self.y_axis)
+        } else {
+            write!(f, "[{}, {}]", self.x_axis, self.y_axis)
+        }
     }
 }
diff --git a/src/f32/scalar/mat3a.rs b/src/f32/scalar/mat3a.rs
index 86f76e5..167c662 100644
--- a/src/f32/scalar/mat3a.rs
+++ b/src/f32/scalar/mat3a.rs
@@ -793,6 +793,14 @@ impl fmt::Debug for Mat3A {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Mat3A {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
+        if let Some(p) = f.precision() {
+            write!(
+                f,
+                "[{:.*}, {:.*}, {:.*}]",
+                p, self.x_axis, p, self.y_axis, p, self.z_axis
+            )
+        } else {
+            write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
+        }
     }
 }
diff --git a/src/f32/scalar/mat4.rs b/src/f32/scalar/mat4.rs
index 0de4514..b39bb3f 100644
--- a/src/f32/scalar/mat4.rs
+++ b/src/f32/scalar/mat4.rs
@@ -1363,10 +1363,18 @@ impl fmt::Debug for Mat4 {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Mat4 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(
-            f,
-            "[{}, {}, {}, {}]",
-            self.x_axis, self.y_axis, self.z_axis, self.w_axis
-        )
+        if let Some(p) = f.precision() {
+            write!(
+                f,
+                "[{:.*}, {:.*}, {:.*}, {:.*}]",
+                p, self.x_axis, p, self.y_axis, p, self.z_axis, p, self.w_axis
+            )
+        } else {
+            write!(
+                f,
+                "[{}, {}, {}, {}]",
+                self.x_axis, self.y_axis, self.z_axis, self.w_axis
+            )
+        }
     }
 }
diff --git a/src/f32/scalar/quat.rs b/src/f32/scalar/quat.rs
index 04009bc..2c548b8 100644
--- a/src/f32/scalar/quat.rs
+++ b/src/f32/scalar/quat.rs
@@ -721,8 +721,16 @@ impl fmt::Debug for Quat {
 
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Quat {
-    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(fmt, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        if let Some(p) = f.precision() {
+            write!(
+                f,
+                "[{:.*}, {:.*}, {:.*}, {:.*}]",
+                p, self.x, p, self.y, p, self.z, p, self.w
+            )
+        } else {
+            write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
+        }
     }
 }
 
diff --git a/src/f32/scalar/vec3a.rs b/src/f32/scalar/vec3a.rs
index 324c023..a5e148e 100644
--- a/src/f32/scalar/vec3a.rs
+++ b/src/f32/scalar/vec3a.rs
@@ -1289,7 +1289,11 @@ impl IndexMut<usize> for Vec3A {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Vec3A {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
+        if let Some(p) = f.precision() {
+            write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
+        } else {
+            write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
+        }
     }
 }
 
diff --git a/src/f32/scalar/vec4.rs b/src/f32/scalar/vec4.rs
index b30bcc3..538a064 100644
--- a/src/f32/scalar/vec4.rs
+++ b/src/f32/scalar/vec4.rs
@@ -1297,7 +1297,15 @@ impl IndexMut<usize> for Vec4 {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Vec4 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
+        if let Some(p) = f.precision() {
+            write!(
+                f,
+                "[{:.*}, {:.*}, {:.*}, {:.*}]",
+                p, self.x, p, self.y, p, self.z, p, self.w
+            )
+        } else {
+            write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
+        }
     }
 }
 
diff --git a/src/f32/sse2/mat2.rs b/src/f32/sse2/mat2.rs
index d84b29a..dda2157 100644
--- a/src/f32/sse2/mat2.rs
+++ b/src/f32/sse2/mat2.rs
@@ -562,6 +562,10 @@ impl fmt::Debug for Mat2 {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Mat2 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "[{}, {}]", self.x_axis, self.y_axis)
+        if let Some(p) = f.precision() {
+            write!(f, "[{:.*}, {:.*}]", p, self.x_axis, p, self.y_axis)
+        } else {
+            write!(f, "[{}, {}]", self.x_axis, self.y_axis)
+        }
     }
 }
diff --git a/src/f32/sse2/mat3a.rs b/src/f32/sse2/mat3a.rs
index 4475c04..730f10e 100644
--- a/src/f32/sse2/mat3a.rs
+++ b/src/f32/sse2/mat3a.rs
@@ -800,6 +800,14 @@ impl fmt::Debug for Mat3A {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Mat3A {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
+        if let Some(p) = f.precision() {
+            write!(
+                f,
+                "[{:.*}, {:.*}, {:.*}]",
+                p, self.x_axis, p, self.y_axis, p, self.z_axis
+            )
+        } else {
+            write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
+        }
     }
 }
diff --git a/src/f32/sse2/mat4.rs b/src/f32/sse2/mat4.rs
index b3173f5..4da52e8 100644
--- a/src/f32/sse2/mat4.rs
+++ b/src/f32/sse2/mat4.rs
@@ -1460,10 +1460,18 @@ impl fmt::Debug for Mat4 {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Mat4 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(
-            f,
-            "[{}, {}, {}, {}]",
-            self.x_axis, self.y_axis, self.z_axis, self.w_axis
-        )
+        if let Some(p) = f.precision() {
+            write!(
+                f,
+                "[{:.*}, {:.*}, {:.*}, {:.*}]",
+                p, self.x_axis, p, self.y_axis, p, self.z_axis, p, self.w_axis
+            )
+        } else {
+            write!(
+                f,
+                "[{}, {}, {}, {}]",
+                self.x_axis, self.y_axis, self.z_axis, self.w_axis
+            )
+        }
     }
 }
diff --git a/src/f32/sse2/quat.rs b/src/f32/sse2/quat.rs
index 2474577..10a619c 100644
--- a/src/f32/sse2/quat.rs
+++ b/src/f32/sse2/quat.rs
@@ -772,8 +772,16 @@ impl fmt::Debug for Quat {
 
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Quat {
-    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(fmt, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        if let Some(p) = f.precision() {
+            write!(
+                f,
+                "[{:.*}, {:.*}, {:.*}, {:.*}]",
+                p, self.x, p, self.y, p, self.z, p, self.w
+            )
+        } else {
+            write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
+        }
     }
 }
 
diff --git a/src/f32/sse2/vec3a.rs b/src/f32/sse2/vec3a.rs
index dffe0d7..72d66f5 100644
--- a/src/f32/sse2/vec3a.rs
+++ b/src/f32/sse2/vec3a.rs
@@ -1227,7 +1227,11 @@ impl IndexMut<usize> for Vec3A {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Vec3A {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
+        if let Some(p) = f.precision() {
+            write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
+        } else {
+            write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
+        }
     }
 }
 
diff --git a/src/f32/sse2/vec4.rs b/src/f32/sse2/vec4.rs
index e724ff8..f97884f 100644
--- a/src/f32/sse2/vec4.rs
+++ b/src/f32/sse2/vec4.rs
@@ -1142,7 +1142,15 @@ impl IndexMut<usize> for Vec4 {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Vec4 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
+        if let Some(p) = f.precision() {
+            write!(
+                f,
+                "[{:.*}, {:.*}, {:.*}, {:.*}]",
+                p, self.x, p, self.y, p, self.z, p, self.w
+            )
+        } else {
+            write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
+        }
     }
 }
 
diff --git a/src/f32/vec2.rs b/src/f32/vec2.rs
index e826170..4154a2d 100644
--- a/src/f32/vec2.rs
+++ b/src/f32/vec2.rs
@@ -1191,7 +1191,11 @@ impl IndexMut<usize> for Vec2 {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Vec2 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "[{}, {}]", self.x, self.y)
+        if let Some(p) = f.precision() {
+            write!(f, "[{:.*}, {:.*}]", p, self.x, p, self.y)
+        } else {
+            write!(f, "[{}, {}]", self.x, self.y)
+        }
     }
 }
 
diff --git a/src/f32/vec3.rs b/src/f32/vec3.rs
index a5f0248..1c3314b 100644
--- a/src/f32/vec3.rs
+++ b/src/f32/vec3.rs
@@ -1280,7 +1280,11 @@ impl IndexMut<usize> for Vec3 {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Vec3 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
+        if let Some(p) = f.precision() {
+            write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
+        } else {
+            write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
+        }
     }
 }
 
diff --git a/src/f32/wasm32/mat2.rs b/src/f32/wasm32/mat2.rs
index b8ecd23..fc2d0a6 100644
--- a/src/f32/wasm32/mat2.rs
+++ b/src/f32/wasm32/mat2.rs
@@ -536,6 +536,10 @@ impl fmt::Debug for Mat2 {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Mat2 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "[{}, {}]", self.x_axis, self.y_axis)
+        if let Some(p) = f.precision() {
+            write!(f, "[{:.*}, {:.*}]", p, self.x_axis, p, self.y_axis)
+        } else {
+            write!(f, "[{}, {}]", self.x_axis, self.y_axis)
+        }
     }
 }
diff --git a/src/f32/wasm32/mat3a.rs b/src/f32/wasm32/mat3a.rs
index b1fdbf5..a8d6a8a 100644
--- a/src/f32/wasm32/mat3a.rs
+++ b/src/f32/wasm32/mat3a.rs
@@ -795,6 +795,14 @@ impl fmt::Debug for Mat3A {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Mat3A {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
+        if let Some(p) = f.precision() {
+            write!(
+                f,
+                "[{:.*}, {:.*}, {:.*}]",
+                p, self.x_axis, p, self.y_axis, p, self.z_axis
+            )
+        } else {
+            write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
+        }
     }
 }
diff --git a/src/f32/wasm32/mat4.rs b/src/f32/wasm32/mat4.rs
index 3d28ed5..c80e962 100644
--- a/src/f32/wasm32/mat4.rs
+++ b/src/f32/wasm32/mat4.rs
@@ -1451,10 +1451,18 @@ impl fmt::Debug for Mat4 {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Mat4 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(
-            f,
-            "[{}, {}, {}, {}]",
-            self.x_axis, self.y_axis, self.z_axis, self.w_axis
-        )
+        if let Some(p) = f.precision() {
+            write!(
+                f,
+                "[{:.*}, {:.*}, {:.*}, {:.*}]",
+                p, self.x_axis, p, self.y_axis, p, self.z_axis, p, self.w_axis
+            )
+        } else {
+            write!(
+                f,
+                "[{}, {}, {}, {}]",
+                self.x_axis, self.y_axis, self.z_axis, self.w_axis
+            )
+        }
     }
 }
diff --git a/src/f32/wasm32/quat.rs b/src/f32/wasm32/quat.rs
index c9dc2f8..c09422b 100644
--- a/src/f32/wasm32/quat.rs
+++ b/src/f32/wasm32/quat.rs
@@ -761,8 +761,16 @@ impl fmt::Debug for Quat {
 
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Quat {
-    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(fmt, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        if let Some(p) = f.precision() {
+            write!(
+                f,
+                "[{:.*}, {:.*}, {:.*}, {:.*}]",
+                p, self.x, p, self.y, p, self.z, p, self.w
+            )
+        } else {
+            write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
+        }
     }
 }
 
diff --git a/src/f32/wasm32/vec3a.rs b/src/f32/wasm32/vec3a.rs
index 22a9e14..8f59ddd 100644
--- a/src/f32/wasm32/vec3a.rs
+++ b/src/f32/wasm32/vec3a.rs
@@ -1191,7 +1191,11 @@ impl IndexMut<usize> for Vec3A {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Vec3A {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
+        if let Some(p) = f.precision() {
+            write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
+        } else {
+            write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
+        }
     }
 }
 
diff --git a/src/f32/wasm32/vec4.rs b/src/f32/wasm32/vec4.rs
index 58be199..0075de8 100644
--- a/src/f32/wasm32/vec4.rs
+++ b/src/f32/wasm32/vec4.rs
@@ -1113,7 +1113,15 @@ impl IndexMut<usize> for Vec4 {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for Vec4 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
+        if let Some(p) = f.precision() {
+            write!(
+                f,
+                "[{:.*}, {:.*}, {:.*}, {:.*}]",
+                p, self.x, p, self.y, p, self.z, p, self.w
+            )
+        } else {
+            write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
+        }
     }
 }
 
diff --git a/src/f64/daffine2.rs b/src/f64/daffine2.rs
index cf9fe5f..2853dd2 100644
--- a/src/f64/daffine2.rs
+++ b/src/f64/daffine2.rs
@@ -346,11 +346,19 @@ impl core::fmt::Debug for DAffine2 {
 #[cfg(not(target_arch = "spirv"))]
 impl core::fmt::Display for DAffine2 {
     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
-        write!(
-            f,
-            "[{}, {}, {}]",
-            self.matrix2.x_axis, self.matrix2.y_axis, self.translation
-        )
+        if let Some(p) = f.precision() {
+            write!(
+                f,
+                "[{:.*}, {:.*}, {:.*}]",
+                p, self.matrix2.x_axis, p, self.matrix2.y_axis, p, self.translation
+            )
+        } else {
+            write!(
+                f,
+                "[{}, {}, {}]",
+                self.matrix2.x_axis, self.matrix2.y_axis, self.translation
+            )
+        }
     }
 }
 
diff --git a/src/f64/daffine3.rs b/src/f64/daffine3.rs
index 6a261bf..c5ceb08 100644
--- a/src/f64/daffine3.rs
+++ b/src/f64/daffine3.rs
@@ -484,11 +484,26 @@ impl core::fmt::Debug for DAffine3 {
 #[cfg(not(target_arch = "spirv"))]
 impl core::fmt::Display for DAffine3 {
     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
-        write!(
-            f,
-            "[{}, {}, {}, {}]",
-            self.matrix3.x_axis, self.matrix3.y_axis, self.matrix3.z_axis, self.translation
-        )
+        if let Some(p) = f.precision() {
+            write!(
+                f,
+                "[{:.*}, {:.*}, {:.*}, {:.*}]",
+                p,
+                self.matrix3.x_axis,
+                p,
+                self.matrix3.y_axis,
+                p,
+                self.matrix3.z_axis,
+                p,
+                self.translation
+            )
+        } else {
+            write!(
+                f,
+                "[{}, {}, {}, {}]",
+                self.matrix3.x_axis, self.matrix3.y_axis, self.matrix3.z_axis, self.translation
+            )
+        }
     }
 }
 
diff --git a/src/f64/dmat2.rs b/src/f64/dmat2.rs
index 208d158..b1e975d 100644
--- a/src/f64/dmat2.rs
+++ b/src/f64/dmat2.rs
@@ -500,6 +500,10 @@ impl fmt::Debug for DMat2 {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for DMat2 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "[{}, {}]", self.x_axis, self.y_axis)
+        if let Some(p) = f.precision() {
+            write!(f, "[{:.*}, {:.*}]", p, self.x_axis, p, self.y_axis)
+        } else {
+            write!(f, "[{}, {}]", self.x_axis, self.y_axis)
+        }
     }
 }
diff --git a/src/f64/dmat3.rs b/src/f64/dmat3.rs
index f137902..7c9564d 100644
--- a/src/f64/dmat3.rs
+++ b/src/f64/dmat3.rs
@@ -783,6 +783,14 @@ impl fmt::Debug for DMat3 {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for DMat3 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
+        if let Some(p) = f.precision() {
+            write!(
+                f,
+                "[{:.*}, {:.*}, {:.*}]",
+                p, self.x_axis, p, self.y_axis, p, self.z_axis
+            )
+        } else {
+            write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
+        }
     }
 }
diff --git a/src/f64/dmat4.rs b/src/f64/dmat4.rs
index 0ec45ce..d1d10b2 100644
--- a/src/f64/dmat4.rs
+++ b/src/f64/dmat4.rs
@@ -1327,10 +1327,18 @@ impl fmt::Debug for DMat4 {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for DMat4 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(
-            f,
-            "[{}, {}, {}, {}]",
-            self.x_axis, self.y_axis, self.z_axis, self.w_axis
-        )
+        if let Some(p) = f.precision() {
+            write!(
+                f,
+                "[{:.*}, {:.*}, {:.*}, {:.*}]",
+                p, self.x_axis, p, self.y_axis, p, self.z_axis, p, self.w_axis
+            )
+        } else {
+            write!(
+                f,
+                "[{}, {}, {}, {}]",
+                self.x_axis, self.y_axis, self.z_axis, self.w_axis
+            )
+        }
     }
 }
diff --git a/src/f64/dquat.rs b/src/f64/dquat.rs
index 946721b..e2401dc 100644
--- a/src/f64/dquat.rs
+++ b/src/f64/dquat.rs
@@ -703,8 +703,16 @@ impl fmt::Debug for DQuat {
 
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for DQuat {
-    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(fmt, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        if let Some(p) = f.precision() {
+            write!(
+                f,
+                "[{:.*}, {:.*}, {:.*}, {:.*}]",
+                p, self.x, p, self.y, p, self.z, p, self.w
+            )
+        } else {
+            write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
+        }
     }
 }
 
diff --git a/src/f64/dvec2.rs b/src/f64/dvec2.rs
index ecd27a1..3661b15 100644
--- a/src/f64/dvec2.rs
+++ b/src/f64/dvec2.rs
@@ -1191,7 +1191,11 @@ impl IndexMut<usize> for DVec2 {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for DVec2 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "[{}, {}]", self.x, self.y)
+        if let Some(p) = f.precision() {
+            write!(f, "[{:.*}, {:.*}]", p, self.x, p, self.y)
+        } else {
+            write!(f, "[{}, {}]", self.x, self.y)
+        }
     }
 }
 
diff --git a/src/f64/dvec3.rs b/src/f64/dvec3.rs
index aedcbc1..c96f89c 100644
--- a/src/f64/dvec3.rs
+++ b/src/f64/dvec3.rs
@@ -1287,7 +1287,11 @@ impl IndexMut<usize> for DVec3 {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for DVec3 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
+        if let Some(p) = f.precision() {
+            write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
+        } else {
+            write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
+        }
     }
 }
 
diff --git a/src/f64/dvec4.rs b/src/f64/dvec4.rs
index 4a5d06c..d60e4b7 100644
--- a/src/f64/dvec4.rs
+++ b/src/f64/dvec4.rs
@@ -1286,7 +1286,15 @@ impl IndexMut<usize> for DVec4 {
 #[cfg(not(target_arch = "spirv"))]
 impl fmt::Display for DVec4 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
+        if let Some(p) = f.precision() {
+            write!(
+                f,
+                "[{:.*}, {:.*}, {:.*}, {:.*}]",
+                p, self.x, p, self.y, p, self.z, p, self.w
+            )
+        } else {
+            write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
+        }
     }
 }
 


Test patch:
diff --git a/tests/affine2.rs b/tests/affine2.rs
index 205caa4..b80916b 100644
--- a/tests/affine2.rs
+++ b/tests/affine2.rs
@@ -232,6 +232,7 @@ macro_rules! impl_affine2_tests {
         glam_test!(test_affine2_fmt, {
             let a = $affine2::from_cols_array_2d(&MATRIX2D);
             assert_eq!(format!("{}", a), "[[1, 2], [3, 4], [5, 6]]");
+            assert_eq!(format!("{:.1}", a), "[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]");
         });
 
         glam_test!(test_affine2_to_from_slice, {
diff --git a/tests/affine3.rs b/tests/affine3.rs
index 49c4391..5394ef2 100644
--- a/tests/affine3.rs
+++ b/tests/affine3.rs
@@ -309,6 +309,10 @@ macro_rules! impl_affine3_tests {
                 format!("{}", a),
                 "[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]"
             );
+            assert_eq!(
+                format!("{:.1}", a),
+                "[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0], [10.0, 11.0, 12.0]]"
+            );
         });
 
         glam_test!(test_affine3_to_from_slice, {
diff --git a/tests/mat2.rs b/tests/mat2.rs
index 1c11cfb..dddc12c 100644
--- a/tests/mat2.rs
+++ b/tests/mat2.rs
@@ -193,6 +193,7 @@ macro_rules! impl_mat2_tests {
         glam_test!(test_mat2_fmt, {
             let a = $mat2::from_cols_array_2d(&MATRIX);
             assert_eq!(format!("{}", a), "[[1, 2], [3, 4]]");
+            assert_eq!(format!("{:.2}", a), "[[1.00, 2.00], [3.00, 4.00]]");
         });
 
         glam_test!(test_mat2_to_from_slice, {
diff --git a/tests/mat3.rs b/tests/mat3.rs
index 376815b..4a2cf72 100644
--- a/tests/mat3.rs
+++ b/tests/mat3.rs
@@ -332,6 +332,10 @@ macro_rules! impl_mat3_tests {
         glam_test!(test_mat3_fmt, {
             let a = $mat3::from_cols_array_2d(&MATRIX);
             assert_eq!(format!("{}", a), "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]");
+            assert_eq!(
+                format!("{:.1}", a),
+                "[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]"
+            );
         });
 
         glam_test!(test_mat3_to_from_slice, {
diff --git a/tests/mat4.rs b/tests/mat4.rs
index f6109f5..61a5db0 100644
--- a/tests/mat4.rs
+++ b/tests/mat4.rs
@@ -638,6 +638,10 @@ macro_rules! impl_mat4_tests {
                 format!("{}", a),
                 "[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]"
             );
+            assert_eq!(
+                format!("{:.1}", a),
+                "[[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], [9.0, 10.0, 11.0, 12.0], [13.0, 14.0, 15.0, 16.0]]"
+            );
         });
 
         glam_test!(test_mat4_to_from_slice, {
diff --git a/tests/quat.rs b/tests/quat.rs
index dfa7afa..7026246 100644
--- a/tests/quat.rs
+++ b/tests/quat.rs
@@ -340,6 +340,7 @@ macro_rules! impl_quat_tests {
             //     "$quat(\n    1.0,\n    2.0,\n    3.0,\n    4.0\n)"
             // );
             assert_eq!(format!("{}", a), "[0, 0, 0, 1]");
+            assert_eq!(format!("{:.2}", a), "[0.00, 0.00, 0.00, 1.00]");
         });
 
         glam_test!(test_identity, {
diff --git a/tests/vec2.rs b/tests/vec2.rs
index f36f883..e8044b5 100644
--- a/tests/vec2.rs
+++ b/tests/vec2.rs
@@ -920,6 +920,11 @@ macro_rules! impl_vec2_float_tests {
             );
         });
 
+        glam_test!(test_fmt_float, {
+            let a = $vec2::new(1.0, 2.0);
+            assert_eq!(format!("{:.2}", a), "[1.00, 2.00]");
+        });
+
         glam_test!(test_angle_conversion, {
             let angle = 0.;
             let vec = $vec2::from_angle(angle);
diff --git a/tests/vec3.rs b/tests/vec3.rs
index 108d499..8673056 100644
--- a/tests/vec3.rs
+++ b/tests/vec3.rs
@@ -1071,6 +1071,11 @@ macro_rules! impl_vec3_float_tests {
                 $vec3::new(-0.5, 1.0, -5.0)
             );
         });
+
+        glam_test!(test_fmt_float, {
+            let a = $vec3::new(1.0, 2.0, 3.0);
+            assert_eq!(format!("{:.2}", a), "[1.00, 2.00, 3.00]");
+        });
     };
 }
 
diff --git a/tests/vec4.rs b/tests/vec4.rs
index 29fdbac..bc70a74 100644
--- a/tests/vec4.rs
+++ b/tests/vec4.rs
@@ -1168,6 +1168,11 @@ macro_rules! impl_vec4_float_tests {
                 $vec4::new(-0.5, 1.0, -5.0, -1.0)
             );
         });
+
+        glam_test!(test_fmt_float, {
+            let a = $vec4::new(1.0, 2.0, 3.0, 4.0);
+            assert_eq!(format!("{:.2}", a), "[1.00, 2.00, 3.00, 4.00]");
+        });
     };
 }