- MapStruct - Home
- MapStruct - Overview
- MapStruct - Environment Setup
- MapStruct - Mapping
- MapStruct - Basic Mapping
- MapStruct - Custom Mapping
- MapStruct - Mapping Multiple
- MapStruct - Mapping Nested Bean
- MapStruct - Mapping Direct Field
- MapStruct - Builder
- MapStruct - Data Type Conversions
- MapStruct - Implicit Type Conversion
- MapStruct - Using numberFormat
- MapStruct - Using dateFormat
- MapStruct - Using expression
- MapStruct - Using constant
- MapStruct - Using defaultValue
- MapStruct - Using defaultExpression
- MapStruct - Mapping Collections
- MapStruct - Mapping List
- MapStruct - Mapping Map
- Mapstruct - Miscellaneous
- MapStruct - Mapping Streams
- MapStruct - Mapping Enum
- MapStruct - Throwing Exceptions
- MapStruct - Customizing Mapper
- MapStruct - Useful Resources
- MapStruct - Quick Guide
- MapStruct - Useful Resources
- MapStruct - Discussion
MapStruct - Using numberFormat
MapStruct handles conversion of numbers to String in required format seamlessly. We can pass the required format as numberFormat during @Mapping annotation. For example, consider a case where an amount stored in numbers is to be shown in currency format.
Source − Entity has price as 350.
Target − Model to show price as $350.00.
numberFormat − Use format $#.00
Example - Using numberFormat
Open project mapping as updated in Mapping Implicit Type Conversions chapter in Eclipse.
Create CarEntity.java with following code −
CarEntity.java
package com.tutorialspoint.entity; public class CarEntity { private int id; private double price; public int getId() { return id; } public void setId(int id) { this.id = id; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }
Create Car.java with following code −
Car.java
package com.tutorialspoint.model; public class Car { private int id; private String price; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } }
Create CarMapper.java with following code −
CarMapper.java
package com.tutorialspoint.mapper; import org.mapstruct.Mapper; import org.mapstruct.Mapping; import com.tutorialspoint.entity.CarEntity; import com.tutorialspoint.model.Car; @Mapper public interface CarMapper { @Mapping(source = "price", target = "price", numberFormat = "$#.00") Car getModelFromEntity(CarEntity carEntity); }
Create CarMapperTest.java with following code −
CarMapperTest.java
package com.tutorialspoint.test; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; import org.mapstruct.factory.Mappers; import com.tutorialspoint.entity.CarEntity; import com.tutorialspoint.mapper.CarMapper; import com.tutorialspoint.model.Car; public class CarMapperTest { private CarMapper carMapper = Mappers.getMapper(CarMapper.class); @Test public void testEntityToModel() { CarEntity entity = new CarEntity(); entity.setPrice(345000); entity.setId(1); Car model = carMapper.getModelFromEntity(entity); assertEquals(model.getPrice(), "$345000.00"); assertEquals(entity.getId(), model.getId()); } }
Run the following command to test the mappings.
mvn clean test
Output
Once command is successful. Verify the output.
mvn clean test [INFO] Scanning for projects... ... [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.tutorialspoint.test.[1mCarMapperTest[m [INFO] [1;32mTests run: [0;1;32m1[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.033 s -- in com.tutorialspoint.test.[1mCarMapperTest[m [INFO] Running com.tutorialspoint.test.[1mDeliveryAddressMapperTest[m [INFO] [1;32mTests run: [0;1;32m1[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.007 s -- in com.tutorialspoint.test.[1mDeliveryAddressMapperTest[m [INFO] Running com.tutorialspoint.test.[1mStudentMapperTest[m [INFO] [1;32mTests run: [0;1;32m2[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 s -- in com.tutorialspoint.test.[1mStudentMapperTest[m [INFO] [INFO] Results: [INFO] [INFO] [1;32mTests run: 4, Failures: 0, Errors: 0, Skipped: 0[m [INFO] [INFO] [1m------------------------------------------------------------------------[m [INFO] [1;32mBUILD SUCCESS[m [INFO] [1m------------------------------------------------------------------------[m [INFO] Total time: 3.349 s [INFO] Finished at: 2025-09-13T20:53:33+05:30 [INFO] [1m------------------------------------------------------------------------[m
Advertisements